LeetCode (45) 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.)
分析
该题目是LeetCode 55 Jump Game的延伸题目, 不仅需要判断能否到达最终地点,而且需要计算出进行的最小跳数。
该题目,调试了几次都没AC,最终参考别人的思想才过,羞愧。。。
ret:目前为止的jump数
curRch:从A[0]进行ret次jump之后达到的最大范围
curMax:从0~i这i+1个A元素中能达到的最大范围
当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素,因此需要增加一次jump,使之达到
记录的curMax。
AC代码
class Solution {
public:
int jump(vector<int>& nums) {
int ret = 0;
int curMax = 0;
int curRch = 0;
int n = nums.size();
for(int i = 0; i < n; i ++)
{
if(curRch < i)
{
ret ++;
curRch = curMax;
}
curMax = max(curMax, nums[i]+i);
}
return ret;
}
};
LeetCode (45) Jump Game II的更多相关文章
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(55)Jump Game
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- LeetCode(47):全排列 II
Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- LeetCode(63)Unique Paths II
题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...
随机推荐
- 莫比乌斯反演总结——Chemist
懵逼乌斯反演果然名不虚传,自闭了两天的我打算学习一下这一块比较实用的数论内容. (注:1.为了区分狄尼克雷卷积与乘法,本篇文章中乘号全部省略,卷积全部用" * "表示.2.用gcd ...
- windows 命令行下 切换目录
cd D:\ 没啥用,直接D:就可以切换了,D,D:\都不行
- [USACO 2012 Open Gold] Bookshelf【优化dp】
传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=138 传送门2:http://www.lydsy.com/JudgeOn ...
- 二分查找+数学 HDOJ 4342 History repeat itself
题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ...
- iOS9导入高德地图报错App Transport Security has blocked...
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Te ...
- 172 Factorial Trailing Zeroes 阶乘后的零
给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...
- [转]在 Azure 云服务上设计大规模服务的最佳实践
本文转自:http://technet.microsoft.com/zh-cn/magazine/jj717232.aspx 英文版:http://msdn.microsoft.com/library ...
- 记录一下java在后端用request来判断请求类型
这几天看代码,看到这么一个操作. String requestType = request.getHeader("X-Requested-With"); 于是各种查找 这里记 ...
- mybatis的mapper.xml文件细节
- 11 DOM基础
1.css 标签 js 元素 dom 节点,元素节点,属性节点,文本节点 2.dom浏览器支持率 ie 10% chrome 60% FF 99% 3. ...