【LeetCode】55. Jump Game 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/jump-game/description/
题目描述
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.
题目大意
每个位置上的数字代表了最多可以向右跳多少步,问能不能跳到最右边的位置。
解题方法
贪心
这个题的tag是贪心,贪心策略是我们每次都选取最优的策略,然后前面已经选好了的就不用管了。
这个题的贪心方法是,我们使用一个变量reach保存当前能到达的最后位置索引,那么在每个位置的时候判断这个位置能不能到达,即位置的索引大于了reach说明前面无论怎么走也走不到这个位置,就返回False好了。如果这个位置可以到达,那么要维护一下这个reach,更新策略是当前位置索引+这个数字代表的能向右走多少步,这个代表了到达当前位置的时候向右能到达的最远距离,在这个最远距离以内的任何位置都能到,因为每次跳的步数可以变小的。那么进行了这么一次循环以后,每个位置都判断为能到达,所以结果返回True就好了。
时间复杂度是O(N),空间复杂度是O(1).
Python代码如下:
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
reach = 0
for i, num in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + num)
return True
C++代码如下:
class Solution {
public:
bool canJump(vector<int>& nums) {
const int M = nums.size();
int reach = 0;
for (int i = 0; i < M; ++i) {
if (i > reach) return false;
reach = max(nums[i] + i, reach);
}
return reach >= M - 1;
}
};
贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来最好的选择。也就是说,不从整体最优上加以考虑,他所作出的是在某种意义上的局部最优解。贪心算法和动态规划算法都是由局部最优导出全局最优,这里不得不比较下二者的区别。
贪心算法:
1.贪心算法中,作出的每步贪心决策都无法改变,因为贪心策略是由上一步的最优解推导下一步的最优解,而上一步之前的最优解则不作保留;
2.由(1)中的介绍,可以知道贪心法正确的条件是:每一步的最优解一定包含上一步的最优解。
动态规划算法:
1.全局最优解中一定包含某个局部最优解,但不一定包含前一个局部最优解,因此需要记录之前的所有最优解;
2.动态规划的关键是状态转移方程,即如何由以求出的局部最优解来推导全局最优解;
3.边界条件:即最简单的,可以直接得出的局部最优解。
参考资料:
日期
2018 年 10 月 28 日 —— 10月份最后一个周一
2019 年 1 月 11 日 —— 小光棍节?
【LeetCode】55. Jump Game 解题报告(Python & C++)的更多相关文章
- [LeetCode] 55. Jump Game 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- perl练习——计算点突变
题目来源:http://rosalind.info/problems/hamm/ 一.程序目的:计算序列点突变(Point Mutations) 输入: GAGCCTACTAACGGGAT CATCG ...
- 11.13python第一周周末练习
2.请输出你的基本个人信息 3.结合逻辑判断,写一个不同学生分数,输出良好,优秀,分数不及格 循环输出 字符串的替换. 以什么开头startwith 以什么结尾endwith 列表转为字符串 字符串转 ...
- day11 序列化组件、批量出入、自定义分页器
day11 序列化组件.批量出入.自定义分页器 今日内容详细 ajax实现删除二次提醒(普通版本) ajax结合第三方插件sweetalert实现二次提醒(样式好看些) ajax如何发送文件数据 aj ...
- Learning Spark中文版--第五章--加载保存数据(1)
开发工程师和数据科学家都会受益于本章的部分内容.工程师可能希望探索更多的输出格式,看看有没有一些适合他们下游用户的格式.数据科学家可能会更关注他们已经使用的数据格式. Motivation 我 ...
- eclipse上点击open Perspective找不到java EE的解决办法
原因:没有安装java ee等插件 Help--->Install New software---->work with中选择All Available Sites----> ...
- Rust 总章
1.1 Rust安装 3.5 Rust Generic Types, Traits, and Lifetimes 3.6 String 与 切片&str的区别 https://openslr. ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Linux学习 - 输入输出重定向,管道符,通配符
一.键盘输入读取read read [选项] [变量名] -p [显示信息] 在等待read输入时,输出提示信息 -t [秒数] 指定read输入等待时间 -n [字符数] 指定read只接收n个字符 ...
- Java 8实现BASE64编解码
Java一直缺少BASE64编码 API,以至于通常在项目开发中会选用第三方的API实现.但是,Java 8实现了BASE64编解码API,它包含到java.util包.下面我会对Java 8的BAS ...
- 超过三张表禁止join
一. 问题提出 <阿里巴巴JAVA开发手册>里面写超过三张表禁止join,这是为什么? 二.问题分析 对这个结论,你是否有怀疑呢?也不知道是哪位先哲说的不要人云亦云,今天我设计sql,来验 ...