leetcode面试准备: Jump Game II
1 题目
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 is2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
接口:public int jump(int[] A);
2 思路
从数字的0位置,开始往后挑,求能够达到最后一个元素的最小跳数。
贪心思想:和Jump Game类似,
用maxReach表示能够走到的最远的index,不断更新这个全局最值。所以,有局部最值。
局部最值: maxLocal = A[i] + i,表示走到i层时,能够达到的最远的index。
step: 走到当前的最小步数
lastReach: 关键变量的引入,在0——lastReach之间的层数,都不会增加step的步数。如何更新这个值成了问题的关键—— 遍历的层数i > lastReach的时候。
复杂度: Time:O(n) Space: O(1)
3 代码
public int jump(int[] A) {
int len = A.length;
int step = 0, lastReach = 0, maxReach = 0;
for (int i = 0; (i <= maxReach) && (i < len); i++) {
int localReach = A[i] + i;
if (i > lastReach) {
step++;
lastReach = maxReach;
}
maxReach = maxReach > localReach ? maxReach : localReach;
}
return maxReach < len - 1 ? 0 : step;
}
4 总结
贪心的思想,从Jump Game得到思路:采用局部最值和全局最值。同时增加2个变量来获取最终的结果是关键。
5 扩展
Jump Game
6 参考
leetcode面试准备: Jump Game II的更多相关文章
- leetcode面试准备: Jump Game
1 题目 Given an array of non-negative integers, you are initially positioned at the first index of the ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- 【LeetCode】45. Jump Game II
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【一天一道LeetCode】#45. Jump Game II
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- 【LeetCode】45. Jump Game II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- LeetCode OJ 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode greedy]45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode OJ:Jump Game II(跳跃游戏2)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
随机推荐
- 获取IMEI码
核心代码: Imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId(); 1.加入权限 在manifes ...
- Android(java)学习笔记190:Eclipse中的控制台不停报错Can't bind to local 8700 for debugger
[DDMS] Can't bind to local 8600 for debugger 改成 Under Window -> Preferences -> Android -> D ...
- Linux下载jdk ,Linux如何下载jdk
Linux下载jdk Linux如何下载jdk >>>>>>>>>>>>>>>>>>> ...
- 程序员带你学习安卓开发,十天快速入-对比C#学习java语法
关注今日头条-做全栈攻城狮,学代码也要读书,爱全栈,更爱生活.提供程序员技术及生活指导干货. 如果你真想学习,请评论学过的每篇文章,记录学习的痕迹. 请把所有教程文章中所提及的代码,最少敲写三遍,达到 ...
- everything is nothing
可以选ios,可以选择android ,可以选择javaee,因为不想让自己这段时间的努力没有一个完美的结局.最起码真的能做点东西出来,所以6.10--8.10,两个月把javaee实训的该准备.可以 ...
- PHP获得header头进行分析
学web的人都知道,要深刻的理解就一定要对HTTP协议有深刻的理解,这样你才能理解整个运行的流程,有些功能你才能理解应该 如何去实现,比如:仿盗链啊,定义IP后切换页面语种的版本啊,等等, 这里就来对 ...
- Js 的常用方法:页面跳转,Session,类继承
MyApp.Base = function () { } var basePrototype = MyApp.Base["prototype"]; //对象克隆方法 basePro ...
- Java多线程读书笔记之一
今天开始陆续将这几天跟进Java多线程知识的成果记录下来分享. Java多线程的知识是一直想要系统彻底的看完的,但是懒惰加无聊早就了我每天都没有进展,这回下决心一定要把这块知识系统梳理完. 我的知识来 ...
- XmlHttpRequest 使用
1. IE7以后对xmlHttpRequest 对象的创建在不同浏览器上是兼容的.下面的方法是考虑兼容性的,实际项目中一般使用Jquery的ajax请求,可以不考虑兼容性问题. function ge ...
- jQuery 遍历后代
后代是子.孙.曾孙等等. 通过 jQuery,您能够向下遍历 DOM 树,以查找元素的后代. 向下遍历 DOM 树 下面是两个用于向下遍历 DOM 树的 jQuery 方法: children() f ...