题目:

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

代码:

class Solution {
public:
int jump(vector<int>& nums) {
int max_jump=, next_max_jump=, min_step=;
for ( int i = ; i<=max_jump; ++i )
{
if ( max_jump>=nums.size()- ) break;
if ( max_jump < i+nums[i] ) next_max_jump = std::max(next_max_jump, i+nums[i]);
if ( i==max_jump )
{
max_jump = next_max_jump;
min_step++;
}
}
return min_step;
}
};

tips:

参考Jump Game的Greedy思路。

这道题要求求出所有可能到达路径中的最短步长,为了保持O(n)的解法继续用Greedy。

这道题的核心在于贪心维护两个变量:

max_jump:记录上一次跳跃能跳到最大的下标位置

next_max_jump:记录遍历所有max_jump之前的元素后,下一次可能跳到的最大下标位置

举例说明如下:

原始数组如右边所示:{7,0,9,6,9,6,1,7,9,0,1,2,9,0,3}

初始:max_jump=0 next_max_jump=0 min_step=1

i=0:next_max_jump=7  更新max_jump=7 更新min_step=1

i=1: 各个值不变

i=2: i+nums[i]=2+9=11>7 更新next_max_jump=11

i=3:i+nums[i]=3+6=9<11 不做更新

i=4: i+nums[i]=4+9=13>11 更新max_jump=13

...

i=7:i+nums[i]=7+7=14 >= nums.size()-1 返回min_step=2

完毕

==========================================

第二次过这道题,不太顺,大体复习了下思路。

class Solution {
public:
int jump(vector<int>& nums) {
if ( nums.size()== ) return ;
int ret = ;
int nextJump = ;
int maxLength = ;
for ( int i=; i<=maxLength; ++i )
{
if ( maxLength>=nums.size()- ) break;
nextJump = std::max(i+nums[i], nextJump);
if ( i==maxLength )
{
maxLength = nextJump;
ret++;
}
}
return ret;
}
};

==========================================

第三次过这道题,把代码改了一行,但是整体结构清晰了不少。

class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size()<) return ;
int steps = ;
int local = ;
int next = local;
for ( int i=; i<=local; ++i )
{
next = max(next, i+nums[i]);
if ( i==local )
{
steps++;
local = next;
if ( local>=nums.size()- ) return steps;
}
}
return ;
}
};

next只负责看下一跳能够到哪。

什么时候更新local了,再判断能否跳到尾部。

【Jump Game II 】cpp的更多相关文章

  1. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  2. 【Unique Paths II】cpp

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

  3. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  4. 【Spiral Matrix II】cpp

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  5. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  6. 【Combination Sum II 】cpp

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

  7. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  8. 【Single Num II】cpp

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

  9. 【N-Quens II】cpp

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

随机推荐

  1. MATLAB之易经卜卦程序+GUI

    MATLAB之易经卜卦程序+GUI   日月为易,刚柔相推. 是故易有太极,是生两仪,两仪生四象,四象生八卦,八卦定吉凶,吉凶生大业.是故法象莫大乎天地,变通莫大乎四时,悬象著明莫大乎日月.   本文 ...

  2. ArcGIS for Android 中实现要素绘制时固定MapView

    最近在项目中遇到这么一个情况,在MapView中要求实现绘制点.线.面. 在这里面就会遇到这么一个问题,绘制折线和多边形型时,每点击一个点屏幕就会跟着晃,使用起来很不方便(使用Note2 触控笔),所 ...

  3. [Asp.Net] Global.asax

    Global.asax.cs文件会被编译到对应的dll 但部署是还需要Global.asax文件  class Global中的方法才会在程序启动时执行

  4. 真实场景中WebRTC 用到的服务 STUN, TURN 和 signaling

    FQ收录转自:WebRTC in the real world: STUN, TURN and signaling WebRTC enables peer to peer communication. ...

  5. java Vamei快速教程06 组合

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经尝试去定义类.定义类,就是新建了一种类型(type).有了类,我们接着构造 ...

  6. 如何在Java代码中使用SAP云平台CloudFoundry环境的环境变量

    本文使用的例子源代码在我的github上. 在我的公众号文章在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务介绍了如何通过Cloud Connector ...

  7. Javascript 向量

    向量 既有大小又有方向的量叫做向量(亦称矢量),与标量相对,用JS实现代码如下,直接搬miloyip的了 Vector2 = function(x, y) { this.x = x; this.y = ...

  8. numpy.random.shuffle(x)的用法

    numpy.random.shuffle(x) Modify a sequence in-place by shuffling its contents. Parameters: x : array_ ...

  9. java arraylist int[] 转换

    double no=Double.valueOf("str");int num4=(int)no;double no1=Double.parseDouble("str&q ...

  10. c++question 004 c++基本数据类型有哪些?

    (1)signed int类型 整数型 占内存4个字节 一个字节byte 占8个二进制位 一个整型就占32位 (2)short int  短整型 占内存2个字节 一个短整型占16位 (3)long i ...