题目

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;
}
};

GitHub测试程序源码

LeetCode (45) Jump Game II的更多相关文章

  1. 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 ...

  2. LeetCode(90):子集 II

    Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...

  3. LeetCode(219) Contains Duplicate II

    题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  4. LeetCode(45): 跳跃游戏 II

    Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  5. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  6. LeetCode(55)Jump Game

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  7. LeetCode(47):全排列 II

    Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...

  8. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

  9. LeetCode(63)Unique Paths II

    题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...

随机推荐

  1. vim下撤销操作.选中复制等操作

    vim撤销操作:u vim恢复操作:ctrl+r 使用normal模式下的  v命令,进入visual模式,v+ j/k/h/l   进行文本选中 对于选中的文本进行如下按键: (1.1)d   -- ...

  2. VS 2017 产品密钥

    Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QFVisual Studio 2017(VS2017 ...

  3. A - I'm bored with life

    Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university ...

  4. 题解报告:hdu 2084 数塔(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...

  5. Ghost系统操作记录

    1.下载Symantec Ghost应用. 2.下载老毛桃PE工具箱. 3.利用老毛桃PE工具箱制作启动U盘. 4.拷贝Ghost应用至U盘. 5.设置计算机启动顺序为U盘启动. 6.重启计算机进入P ...

  6. AJPFX简述可变参数概述和使用

    A:可变参数概述 定义方法的时候不知道该定义多少个参数 B:格式 修饰符 返回值类型 方法名(数据类型… 变量名){} C:注意事项: 这里的变量其实是一个数组 如果一个方法有可变参数,并且有多个参数 ...

  7. 表单里的button默认是submit类型

    今天很坑爹,周六一大早加班开始码代码,本来想做数据加密测试,于是乎用tp框架搭建了一个应用环境,二话不说,开始码码. 但,今天一大早就栽坑!直到同事喊吃饭还在坑里出不来!吃完饭继续码,最后码的我想哭o ...

  8. Objective -C Memory Management 内存管理 第一部分

    Objective -C Memory Management  内存管理  第一部分 Memory management is part of a more general problem in pr ...

  9. reStructuredText学习

    reStructuredText学习====================2015年4月1日 学习的最好方法就是尽快动手开始.不断迭代,不断完善. reStructuredText学习v0.1版本. ...

  10. vuex的应用和解决的实际问题

    这是vuex的语法结构内容 简单的理解vuex: new Vue({ // state data () { return { count: 0 } }, // view template: ` < ...