[LeetCode] 55. Jump Game 跳跃游戏
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
类似题目:
参考资料:
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 跳跃游戏的更多相关文章
- 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 ...
 - leetcode 55. Jump Game、45. Jump Game II(贪心)
		
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
 - LeetCode 55. Jump Game (跳跃游戏)
		
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
 - [LeetCode] Jump Game 跳跃游戏
		
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
 - 【LeetCode每天一题】Jump Game(跳跃游戏)
		
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
 - LeetCode(55): 跳跃游戏
		
Medium! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
 - Jump Game 的三种思路 - leetcode 55. Jump Game
		
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
 - [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 ...
 - LeetCode: 55. Jump Game(Medium)
		
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
 
随机推荐
- 修改Hexo自动生成的HTML文件名
			
导读 我们在使用Hexo框架生成静态博客时,其实是将你写好的.md文件输出成HTML文件进行渲染,其中HTML的文件名称就是.md的文件名称. 而我们为了编辑文章方便,为了通过文件名就知道这是哪篇文章 ...
 - [Node.js] TypeScript 实现 sleep 函数
			
看过不少网友的文章, 有各种方法, 但我想要的是一个能线性执行的sleep函数. /** * 等待指定的时间 * @param ms */ static async sleep(ms: number) ...
 - 时间复杂度o(1), o(n), o(logn), o(nlogn)
			
1.时间复杂度o(1), o(n), o(logn), o(nlogn).算法时间复杂度的时候有说o(1), o(n), o(logn), o(nlogn),这是算法的时空复杂度的表示.不仅仅用于表示 ...
 - Immediate Window
			
name="ZFF""ZFF"date=new DateTime(2017,02,03,21,19,45){2/3/2017 21:19:45 PM} Date ...
 - Winforn中使用FastReport实现点击导出按钮PDF预览并弹出另存为对话框
			
场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
 - Python - 模块 - 第十六天
			
Python 模块 在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办法 ...
 - JS读取xml
			
xml文件 <?xml version="1.0" encoding="utf-8"?> <root> <data id=&quo ...
 - Mac如何生成树形目录图--Tree
			
经常可以在网上看到如下图所示的目录树形图,它们是怎么生成的呢? . ├── AppDelegate │ ├── AppDelegate+Extension.swift │ └── AppDelegat ...
 - Native层和so接口和Java层
			
一.Java层加载so文件 Android在Java层加载so的接口是System.loadLibrary()逐级调用的过程: System.loadLibrary()系统源码: 987 pub ...
 - wc项目(node.js实现)
			
一.github地址:https://github.com/Jasminejiamei/homework-wc 二.PSP PSP Personal Software Process Stages 预 ...