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 ...
随机推荐
- MKServerBuilder.psm1
MKServerBuilder.psm1 function Test-ElevatedShell { $user = [Security.Principal.WindowsIdentity]::Get ...
- Eureka Web UI URL(eureka显示主界面路径设定)
http://stackoverflow.com/questions/30200988/spring-cloud-with-eureka-eureka-web-ui-url ************* ...
- 关于js当中一些糟糕的特性
首先,不可否认,js是一门具有许多优秀特性的弱类型语言,但是这门语言在设计之初就投入了工程实践,没有经历严格的实验室测试,以致力于它是如此的粗糙,在相当长的一段时间很不受开发者待见,被视为一门玩具性的 ...
- CSS常见问题及兼容性
CSS常见问题 1 (IE6,7)H5标签兼容 解决方法1:(只显示核心代码) 1<script> ; ; ; ;;;};;;;;;;; ...
- jsp中的注释
jsp中有各种针对不同类型语言的注释,值得注意的是对于标签 <jsp:include/>是需要使用jsp注释"<%----%>", (不能是<!-- ...
- 如何在OpenWRT环境下做开发
1.搭建开发环境 首先,在执行make menuconfig后,会出现下图: 其中,图中红框部分是我定制路由器的系统版本,大家可以根据不同的路由器进行不同的选择:绿框部分表示我们需要编译一个SDK开发 ...
- Windows下的进程【一】
什么是进程?进程就是一个正在运行的程序的实例,由两部分组成: 内核对象.操作系统用内核对象对进程进行管理,内核对象是操作系统保存进程统计信息的地方. 地址空间.其中包含所有可执行文件或DLL模块的代码 ...
- librarynotfoundforlPodsAFNetworking解决放案
http://www.it165.net/pro/html/201503/36422.html
- winform中的chat
百度一下 源代码下载:百度一下
- 小知识 Vector的枚举 和foreach的用法
package com.java.c.votetor.www; import java.util.Enumeration;import java.util.Iterator;import java.u ...