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 ...
随机推荐
- ubuntu 下编译安装 mysql php nginx 及常见错误 (持续添加)
mysql mysql 可以使用mysql 官方提供的apt源进行安装 参见这里 php 安装前先安装一些常见库 sudo apt-get install libpng16-16 libpng16-d ...
- css3新增加的选择器
css3新增加的选择器 一.属性选择器: E[attr] 只要有属性名E[attr=value] 属性名=属性值E[attr~=blue] 包含这个blue整个单词就可以E[attr^=c] 以这个字 ...
- mvc 路由 使用
url 特性路由: 特性路由可以在 controller和action里面自定义路由规则 这种方式比较灵活 缺点就是不能很好的统一管理url 注册特性路由: public static void ...
- jmeter压测app
使用代理的方式,录制app端脚本,之后用jmeter压测就没啥好说的了 1.电脑端谷歌设置本地代理(端口号为8888) 2.jmeter设置HTTP代理服务器(端口号为8888) 3.手机端wifi设 ...
- P2P之UDP穿透NAT原理
首先先介绍一些基本概念: NAT(Network Address Translators),网络地址转换:网络地址转换是在IP地址日益缺乏的情况下产生的,它的主要目的就 ...
- SQL For Xml
最近遇到点棘手的问题,大致如下: 1.数据局格式: 企业名称 排口名称 监测时间 监测因子 a b c pH值 a b c 氨氮 a b c 化学需氧量(COD) 企业名称.排口名称 ...
- iOS设备、Icon、LaunchImage、图片分辨率
iOS设备 iOS设备的屏幕的大小.分辨率以及比例因数(Scale Factor)[1]. iPhone 设备 宽(inch) 高(inch) 对角线(inch) 逻辑分辨率(point) Scale ...
- 生产者与消费者(一)---wait与notify
生产者消费者问题是研究多线程程序时绕不开的经典问题之一,它描述是有一块缓冲区作为仓库,生产者可以将产品放入仓库,消费者则可以从仓库中取走产品.解决生产者/消费者问题的方法可分为两类: (1)采用某种机 ...
- 《cut命令》-linux命令五分钟系列之十九
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- python模块学习 logging
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...