Leetcode jump Game II
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.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
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.)
题目是假设输入数组能满足到达数组末尾的条件,求出最少的跳数。
题目给出的测试用例是[2,3,1,1,4]
第一次从开头跳,则跳得位置为0+A[0]=2,故在位置2
根据贪心策略下次跳的时候应该选择1~2之间跳的最远的位置开始跳
1的位置跳的距离为1+A[1]=4
2的位置跳的距离为2+A[2]=3
故下一步选择从1的位置开始跳刚好能跳到最后结束
class Solution {
public:
int jump(int A[], int n) {
int res = , last = , cur = ;
for(int i = ; i < n; ++ i){
if(i > last) last = cur,res++;
cur = max(cur,A[i]+i);
}
return res;
}
};
Leetcode jump Game II的更多相关文章
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II(贪婪算法)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode–jump game II
1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...
- leetcode Jump Game II python
@link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...
- [Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【To Read】LeetCode | Jump Game II(转载)
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
随机推荐
- 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码
在一个应用的整个开发过程中涉及到了无数的步骤.其中一些是应用的说明,图片的创作,应用的实现,和实现过后的测试阶段.写代码可能组成了这个过程的绝大部分,因为正是它给了应用生命,但是这样还不够,与它同等重 ...
- jenkins使用简记
一.安装 jenkins有多种安装方式,可以使用内嵌的Servlet容器运行,也可以基于Apache运行,也可以安装成Linux或Windows服务. 1.使用 Servlet 容器运行 从官网下载 ...
- 大熊君学习html5系列之------requestAnimationFrame(实现动画的另一种方案)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...
- 第七届山东省ACM省赛
激动人心的省赛终于结束了…平静下来再回头看真的感觉一波三折…先是赛前毫无预兆的查出突发性耳聋…伴随而来的就是左耳听力下降.轻微耳鸣.极个别情况下的头晕…不过这都还好,毕竟药物可以恢复…热身赛只过了一道 ...
- 安装配置LDAP遇到的问题
问题1:安装完启动ldap服务报错: ldap: unrecognized service? 原因在于新版的openldap将服务名改为了slapd,使用service slapd start即可启动 ...
- HttpClient, HttpClientHandler, and WebRequestHandler Explained
原文地址 https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequest ...
- Pycharm 快捷键
Ctrl + Alt + Space 快速导入任意类 Alt + enter键 快速导入模块.创建类 Ctrl + / 注释/取消行注释 Ctrl + Shift + / ...
- python logging模块笔记
1 ) 给logger定制了两个日志级别INFO和DEBUG,分别通过filehandler添加不同输出到不同文件,但如何让DEBUG里只有DEBUG的信息? 答案:可重写DEBUG对应的Fileha ...
- xcode8 info.plist文件中的各种权限。
NSContactsUsageDescription -> 通讯录 NSMicrophoneUsageDescription -> 麦克风 NSPhotoLibraryUsageDescr ...
- 改写js原装的alert样式
1.将下面的js代码单独到一个js文件中,然后在页面中引用 AlertDialog.js //改写js原装的alert样式 var t; var timeclose = 0; var showBack ...