返回介绍

solution / 0000-0099 / 0045.Jump Game II / README_EN

发布于 2024-06-17 01:04:40 字数 5764 浏览 0 评论 0 收藏 0

45. Jump Game II

中文文档

Description

You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].

Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:

  • 0 <= j <= nums[i] and
  • i + j < n

Return _the minimum number of jumps to reach _nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

 

Example 1:

Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [2,3,0,1,4]
Output: 2

 

Constraints:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 1000
  • It's guaranteed that you can reach nums[n - 1].

Solutions

Solution 1: Greedy Algorithm

We can use a variable $mx$ to record the farthest position that can be reached from the current position, a variable $last$ to record the position of the last jump, and a variable $ans$ to record the number of jumps.

Next, we traverse each position $i$ in $[0,..n - 2]$. For each position $i$, we can calculate the farthest position that can be reached from the current position through $i + nums[i]$. We use $mx$ to record this farthest position, that is, $mx = max(mx, i + nums[i])$. Then, we check whether the current position has reached the boundary of the last jump, that is, $i = last$. If it has reached, then we need to make a jump, update $last$ to $mx$, and increase the number of jumps $ans$ by $1$.

Finally, we return the number of jumps $ans$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

Similar problems:

class Solution:
  def jump(self, nums: List[int]) -> int:
    ans = mx = last = 0
    for i, x in enumerate(nums[:-1]):
      mx = max(mx, i + x)
      if last == i:
        ans += 1
        last = mx
    return ans
class Solution {
  public int jump(int[] nums) {
    int ans = 0, mx = 0, last = 0;
    for (int i = 0; i < nums.length - 1; ++i) {
      mx = Math.max(mx, i + nums[i]);
      if (last == i) {
        ++ans;
        last = mx;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int jump(vector<int>& nums) {
    int ans = 0, mx = 0, last = 0;
    for (int i = 0; i < nums.size() - 1; ++i) {
      mx = max(mx, i + nums[i]);
      if (last == i) {
        ++ans;
        last = mx;
      }
    }
    return ans;
  }
};
func jump(nums []int) (ans int) {
  mx, last := 0, 0
  for i, x := range nums[:len(nums)-1] {
    mx = max(mx, i+x)
    if last == i {
      ans++
      last = mx
    }
  }
  return
}
function jump(nums: number[]): number {
  let [ans, mx, last] = [0, 0, 0];
  for (let i = 0; i < nums.length - 1; ++i) {
    mx = Math.max(mx, i + nums[i]);
    if (last === i) {
      ++ans;
      last = mx;
    }
  }
  return ans;
}
impl Solution {
  pub fn jump(nums: Vec<i32>) -> i32 {
    let n = nums.len();
    let mut dp = vec![i32::MAX; n];
    dp[0] = 0;
    for i in 0..n - 1 {
      for j in 1..=nums[i] as usize {
        if i + j >= n {
          break;
        }
        dp[i + j] = dp[i + j].min(dp[i] + 1);
      }
    }
    dp[n - 1]
  }
}
public class Solution {
  public int Jump(int[] nums) {
    int ans = 0, mx = 0, last = 0;
    for (int i = 0; i < nums.Length - 1; ++i) {
      mx = Math.Max(mx, i + nums[i]);
      if (last == i) {
        ++ans;
        last = mx;
      }
    }
    return ans;
  }
}
#define min(a, b) a < b ? a : b
int jump(int* nums, int numsSize) {
  int dp[numsSize];
  for (int i = 0; i < numsSize; i++) {
    dp[i] = numsSize;
  }
  dp[0] = 0;
  for (int i = 0; i < numsSize - 1; i++) {
    for (int j = i + 1; j < (min(i + nums[i] + 1, numsSize)); j++) {
      dp[j] = min(dp[j], dp[i] + 1);
    }
  }
  return dp[numsSize - 1];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文