返回介绍

solution / 0000-0099 / 0055.Jump Game / README_EN

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

55. Jump Game

中文文档

Description

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

Return true_ if you can reach the last index, or _false_ otherwise_.

 

Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

 

Constraints:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 105

Solutions

Solution 1: Greedy

We use a variable $mx$ to maintain the farthest index that can currently be reached, initially $mx = 0$.

We traverse the array from left to right. For each position $i$ we traverse, if $mx < i$, it means that the current position cannot be reached, so we directly return false. Otherwise, the farthest position that we can reach by jumping from position $i$ is $i+nums[i]$, we use $i+nums[i]$ to update the value of $mx$, that is, $mx = \max(mx, i + nums[i])$.

At the end of the traversal, we directly return true.

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 canJump(self, nums: List[int]) -> bool:
    mx = 0
    for i, x in enumerate(nums):
      if mx < i:
        return False
      mx = max(mx, i + x)
    return True
class Solution {
  public boolean canJump(int[] nums) {
    int mx = 0;
    for (int i = 0; i < nums.length; ++i) {
      if (mx < i) {
        return false;
      }
      mx = Math.max(mx, i + nums[i]);
    }
    return true;
  }
}
class Solution {
public:
  bool canJump(vector<int>& nums) {
    int mx = 0;
    for (int i = 0; i < nums.size(); ++i) {
      if (mx < i) {
        return false;
      }
      mx = max(mx, i + nums[i]);
    }
    return true;
  }
};
func canJump(nums []int) bool {
  mx := 0
  for i, x := range nums {
    if mx < i {
      return false
    }
    mx = max(mx, i+x)
  }
  return true
}
function canJump(nums: number[]): boolean {
  let mx: number = 0;
  for (let i = 0; i < nums.length; ++i) {
    if (mx < i) {
      return false;
    }
    mx = Math.max(mx, i + nums[i]);
  }
  return true;
}
impl Solution {
  #[allow(dead_code)]
  pub fn can_jump(nums: Vec<i32>) -> bool {
    let n = nums.len();
    let mut mx = 0;

    for i in 0..n {
      if mx < i {
        return false;
      }
      mx = std::cmp::max(mx, i + (nums[i] as usize));
    }

    true
  }
}
/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canJump = function (nums) {
  let mx = 0;
  for (let i = 0; i < nums.length; ++i) {
    if (mx < i) {
      return false;
    }
    mx = Math.max(mx, i + nums[i]);
  }
  return true;
};
public class Solution {
  public bool CanJump(int[] nums) {
    int mx = 0;
    for (int i = 0; i < nums.Length; ++i) {
      if (mx < i) {
        return false;
      }
      mx = Math.Max(mx, i + nums[i]);
    }
    return true;
  }
}

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

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

发布评论

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