Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

Example 1:

Input: [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: [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.

这道题说的是有一个非负整数的数组,每个数字表示在当前位置的最大跳力(这里的跳力指的是在当前位置为基础上能到达的最远位置),求判断能不能到达最后一个位置,开始博主以为是必须刚好到达最后一个位置,超过了不算,其实是理解题意有误,因为每个位置上的数字表示的是最大的跳力而不是像玩大富翁一样摇骰子摇出几一定要走几。这里可以用动态规划 Dynamic Programming 来解,维护一个一维数组 dp,其中 dp[i] 表示达到i位置时剩余的跳力,若到达某个位置时跳力为负了,说明无法到达该位置。接下来难点就是推导状态转移方程啦,想想啊,到达当前位置的剩余跳力跟什么有关呢,其实是跟上一个位置的剩余跳力(dp 值)和上一个位置新的跳力(nums 数组中的值)有关,这里新的跳力就是原数组中每个位置的数字,因为其代表了以当前位置为起点能到达的最远位置。所以当前位置的剩余跳力(dp 值)和当前位置新的跳力中的较大那个数决定了当前能到的最远距离,而下一个位置的剩余跳力(dp 值)就等于当前的这个较大值减去1,因为需要花一个跳力到达下一个位置,所以就有状态转移方程了:dp[i] = max(dp[i - 1], nums[i - 1]) - 1,如果当某一个时刻 dp 数组的值为负了,说明无法抵达当前位置,则直接返回 false,最后循环结束后直接返回 true  即可,参见代码如下:

解法一:

class Solution {
public:
bool canJump(vector<int>& nums) {
vector<int> dp(nums.size(), );
for (int i = ; i < nums.size(); ++i) {
dp[i] = max(dp[i - ], nums[i - ]) - ;
if (dp[i] < ) return false;
}
return true;
}
};

其实这题最好的解法不是 DP,而是贪婪算法 Greedy Algorithm,因为这里并不是很关心每一个位置上的剩余步数,而只希望知道能否到达末尾,也就是说我们只对最远能到达的位置感兴趣,所以维护一个变量 reach,表示最远能到达的位置,初始化为0。遍历数组中每一个数字,如果当前坐标大于 reach 或者 reach 已经抵达最后一个位置则跳出循环,否则就更新 reach 的值为其和 i + nums[i] 中的较大值,其中 i + nums[i] 表示当前位置能到达的最大位置,参见代码如下:

解法二:

class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size(), reach = ;
for (int i = ; i < n; ++i) {
if (i > reach || reach >= n - ) break;
reach = max(reach, i + nums[i]);
}
return reach >= n - ;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/55

类似题目:

Jump Game II

参考资料:

https://leetcode.com/problems/jump-game/

https://leetcode.com/problems/jump-game/discuss/20917/Linear-and-simple-solution-in-C++

https://leetcode.com/problems/jump-game/discuss/20923/Java-Solution-easy-to-understand

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 55. Jump Game 跳跃游戏的更多相关文章

  1. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

  2. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  3. LeetCode 55. Jump Game (跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. 【LeetCode每天一题】Jump Game(跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. LeetCode(55): 跳跃游戏

    Medium! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...

  7. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

  8. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

随机推荐

  1. LeetCode 189:旋转数组 Rotate Array

    公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...

  2. 在.net中读写config文件的各种方法【转】

    今天谈谈在.net中读写config文件的各种方法. 在这篇博客中,我将介绍各种配置文件的读写操作. 由于内容较为直观,因此没有过多的空道理,只有实实在在的演示代码, 目的只为了再现实战开发中的各种场 ...

  3. 【java】【guava】Google Guava的splitter用法

    Google Guava的splitter,分割字符串的用法 package com.sxd.swapping.guava; import com.google.common.base.CharMat ...

  4. DRF简易了解

    Drf框架 一丶API接口 # 为了在团队内部形成共识.防止个人习惯差异引起的混乱,我们需要找到一种大家都觉得很好的接口实现规范,而且这种规范能够让后端写的接口,用途一目了然,减少双方之间的合作成本. ...

  5. .NET Core 收徒,有缘者,可破瓶颈

    最近感悟天命,偶有所得,故而打算收徒若干,以继吾之传承. 有缘者,可破瓶颈,职场巅峰指日可待. 入门基本要求: 1.工作经验:1年或以上. 2.拜师费用:3999元(RMB). 传承说明: 1.收徒人 ...

  6. idea修改filetype

    settings/editor/file types把不小心改成Text类型的文件的wildcard去掉即可.

  7. CTF挑战赛丨网络内生安全试验场第一季答题赛火热开启

    前期回顾:挑战世界级“人机大战”,更有万元奖金等你来拿 网络内生安全试验场自上线以来,受到了业内的极大重视与关注. 自9月2日报名通道开启后,报名量更是持续高升,上百名精英白帽踊跃报名. 至此,网络内 ...

  8. 通过MES技术居然可以防止制造数据造假?

    近些年来我们经历了太多制造数据造假事件,特别是前段时间曝出的医药制造事件更是将我们群众的愤怒值推到了最高点.不过我们最应当做的是,冷静下来,思考一下各行各业的我们是不是都该做些什么了?毕竟当下一个灾难 ...

  9. ssh关闭服务关闭 nohup

    默认输出 # nohup cmd & 指定输出路径 # nohup cmd > mylog.out 2>&1 &

  10. Lumen5.7快速实现Captcha图片验证码功能

    公司发送短信注册的接口需要防刷,需要一个图形验证码,不考虑收费产品. Lumen5.7+nginx+mysql 使用了这个作者的扩展包,只讲实现.https://github.com/Youngyez ...