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 ...
随机推荐
- springboot(七) 配置嵌入式Servlet容器
github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...
- CF449D Jzzhu and Numbers
题解 刚刚学习了高维前缀和 这道题就肥肠简单了 高维前缀和其实原理肥肠简单 就是每次只考虑一维,然后只做这一维的前缀和 最后求出的就是总前缀和了 那么对于这道题 也就很简单了 发现选择的所有数每一位都 ...
- NDK(18)eclipse 使用C++ STL
1.引用库 在Application.mk 中使用 APP_STL := stlport_static 等. APP_ABI := x86 armeabi APP_PLATFORM := androi ...
- gcc 编译 c++ 程序(转载)
单个源文件生成可执行程序 下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码: /* helloworld.cpp */ #include <iostream& ...
- 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...
- 轻松搞定Spring+quartz的定时任务
1.spring 的定时任务写法有两种:一种是继承工作类,一种是普通的Bean,定时写法有两种写法:一种是以时间间隔启动任务SimpleTriggerBean,一种是以时刻启动任务CronTrigge ...
- url传值的长度限制解决办法
今天写到两个页面传值,刚开始通过url上加参数进行传值, var strLink = "my.asp?str1=" + str1List + "&str2=&qu ...
- vue ---- Object的一些常用的方法
在对象上添加新属性的几种方法: 直接附代码: 法一:Es6扩展运算符添加属性 法二:利用语法Object.assign(target, ...sources) target目标对象.source ...
- JPA调用存储过程
@Transactional public BasAccount findByAccount(String account) { System.out.println(account); Query ...
- android 开源
http://blog.csdn.net/xiaoxiao_job/article/details/45196119?ref=myread MPAndroidChart https://github. ...