LeetCode 题解工作台

恰好移动 k 步到达某一位置的方法数目

给你两个 正 整数 startPos 和 endPos 。最初,你站在 无限 数轴上位置 startPos 处。在一步移动中,你可以向左或者向右移动一个位置。 给你一个正整数 k ,返回从 startPos 出发、 恰好 移动 k 步并到达 endPos 的 不同 方法数目。由于答案可能会很大,返回…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 状态·转移·动态规划

bolt

答案摘要

我们设计一个函数 $dfs(i, j)$,表示当前位置距离目标位置的距离为 ,还剩 步,有多少种方法到达目标位置。那么答案就是 $dfs(abs(startPos - endPos), k)$。 函数 $dfs(i, j)$ 的计算方式如下:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个 整数 startPosendPos 。最初,你站在 无限 数轴上位置 startPos 处。在一步移动中,你可以向左或者向右移动一个位置。

给你一个正整数 k ,返回从 startPos 出发、恰好 移动 k 步并到达 endPos不同 方法数目。由于答案可能会很大,返回对 109 + 7 取余 的结果。

如果所执行移动的顺序不完全相同,则认为两种方法不同。

注意:数轴包含负整数

 

示例 1:

输入:startPos = 1, endPos = 2, k = 3
输出:3
解释:存在 3 种从 1 到 2 且恰好移动 3 步的方法:
- 1 -> 2 -> 3 -> 2.
- 1 -> 2 -> 1 -> 2.
- 1 -> 0 -> 1 -> 2.
可以证明不存在其他方法,所以返回 3 。

示例 2:

输入:startPos = 2, endPos = 5, k = 10
输出:0
解释:不存在从 2 到 5 且恰好移动 10 步的方法。

 

提示:

  • 1 <= startPos, endPos, k <= 1000
lightbulb

解题思路

方法一:记忆化搜索

我们设计一个函数 dfs(i,j)dfs(i, j),表示当前位置距离目标位置的距离为 ii,还剩 jj 步,有多少种方法到达目标位置。那么答案就是 dfs(abs(startPosendPos),k)dfs(abs(startPos - endPos), k)

函数 dfs(i,j)dfs(i, j) 的计算方式如下:

  • 如果 i>ji \gt j 或者 j<0j \lt 0,说明当前位置距离目标位置的距离大于剩余步数,或者剩余步数为负数,此时无法到达目标位置,返回 00
  • 如果 j=0j = 0,说明剩余步数为 00,此时只有当前位置距离目标位置的距离为 00 时才能到达目标位置,否则无法到达目标位置,返回 11 或者 00
  • 否则,当前位置距离目标位置的距离为 ii,还剩 jj 步,那么有两种方法到达目标位置:
    • 向左移动一步,此时当前位置距离目标位置的距离为 i+1i + 1,还剩 j1j - 1 步,方法数为 dfs(i+1,j1)dfs(i + 1, j - 1)
    • 向右移动一步,此时当前位置距离目标位置的距离为 abs(i1)abs(i - 1),还剩 j1j - 1 步,方法数为 dfs(abs(i1),j1)dfs(abs(i - 1), j - 1)
  • 最后,返回两种方法的和对 109+710^9 + 7 取余的结果。

为了避免重复计算,我们使用记忆化搜索,即使用一个二维数组 ff 记录函数 dfs(i,j)dfs(i, j) 的结果,当函数 dfs(i,j)dfs(i, j) 被调用时,如果 f[i][j]f[i][j] 不为 1-1,则直接返回 f[i][j]f[i][j],否则计算 f[i][j]f[i][j] 的值,并返回 f[i][j]f[i][j]

时间复杂度 O(k2)O(k^2),空间复杂度 O(k2)O(k^2)。其中 kk 为题目给定的步数。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def numberOfWays(self, startPos: int, endPos: int, k: int) -> int:
        @cache
        def dfs(i: int, j: int) -> int:
            if i > j or j < 0:
                return 0
            if j == 0:
                return 1 if i == 0 else 0
            return (dfs(i + 1, j - 1) + dfs(abs(i - 1), j - 1)) % mod

        mod = 10**9 + 7
        return dfs(abs(startPos - endPos), k)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate can handle dynamic programming approaches and optimizes for space and time efficiency.

  • question_mark

    The candidate shows an understanding of state transitions and can apply them effectively to solve combinatorics problems.

  • question_mark

    The candidate demonstrates the ability to handle modular arithmetic for large numbers, which is a key aspect of the problem.

warning

常见陷阱

外企场景
  • error

    Not handling the case where the number of left and right moves cannot sum to the required target difference.

  • error

    Overcomplicating the problem by not utilizing dynamic programming efficiently, resulting in unnecessary computations.

  • error

    Failing to apply the modulo operation at each step, leading to potential overflow or incorrect results.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Change the number of steps k to a larger value and analyze performance.

  • arrow_right_alt

    Introduce additional constraints, such as limiting the number of right or left moves.

  • arrow_right_alt

    Extend the problem to multidimensional grids, where you can move in more directions than just left or right.

help

常见问题

外企场景

恰好移动 k 步到达某一位置的方法数目题解:状态·转移·动态规划 | LeetCode #2400 中等