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

题目是假设输入数组能满足到达数组末尾的条件,求出最少的跳数。

题目给出的测试用例是[2,3,1,1,4]

第一次从开头跳,则跳得位置为0+A[0]=2,故在位置2

根据贪心策略下次跳的时候应该选择1~2之间跳的最远的位置开始跳

1的位置跳的距离为1+A[1]=4

2的位置跳的距离为2+A[2]=3

故下一步选择从1的位置开始跳刚好能跳到最后结束

class Solution {
public:
int jump(int A[], int n) {
int res = , last = , cur = ;
for(int i = ; i < n; ++ i){
if(i > last) last = cur,res++;
cur = max(cur,A[i]+i);
}
return res;
}
};

Leetcode jump Game II的更多相关文章

  1. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  2. [LeetCode] Jump Game II 跳跃游戏之二

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

  3. LeetCode——Jump Game II

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [LeetCode] Jump Game II 贪心

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

  5. [LeetCode] Jump Game II(贪婪算法)

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

  6. leetcode–jump game II

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

  7. leetcode Jump Game II python

    @link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...

  8. [Leetcode] jump game ii 跳跃游戏

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

  9. 【To Read】LeetCode | Jump Game II(转载)

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

随机推荐

  1. 创建一个自定义颜色IRgbColor

    后续文章需要用到,很简单的一个小函数 /// <summary> /// 自定义颜色 /// </summary> /// <param name="r&quo ...

  2. HTC学习笔记

    添加一个属性的setter, getter 建立一个页面 <html> <head> <title>TODO supply a title</title> ...

  3. MVC验证session是否过期,在每个action执行之前验证

            protected override void OnActionExecuting(ActionExecutingContext filterContext)         {   ...

  4. jvm的内部体系结构浅析

    转自:http://www.cnblogs.com/evan2012/archive/2012/05/09/2489417.html 1.jvm的内部体系结构浅析 2.jvm的几个运行时数据区域 3. ...

  5. 巧用dimens适配多个分辨率

      让应用自动适配多个分辨率的屏幕,是每个android程序员的基本功,就好像前端工程师熟练编写CSS Hack一样.适配工作中一个重要的工作就是对页面的调整. 对于页面的适配,有很多的方法和技巧.比 ...

  6. opencv常见代码

    http://blog.csdn.net/lyc_daniel/article/details/16883707

  7. Python基础四

    1. 集合 主要作用: 去重 关系测试, 交集\差集\并集\反向(对称)差集   2. 元组 只读列表,只有count, index 2 个方法 作用:如果一些数据不想被人修改, 可以存成元组,比如身 ...

  8. Eclipse自动编译问题

    今天遇到一个很郁闷的问题,在程序中修改了一个String字符串,结果打断点是发现,还是修改之前的值,一点都没有变,最终发现该类在tomcat中的class的大小一直都没有变,只有修改时间在变,这才意识 ...

  9. linux编程问题记录

    1.程序中需要用到字符串的时候,尽可能选择string类型,这种类型的字符串有很多比较容易的功能,如字符串之间可以直接拷贝赋值 string a; string b="123"; ...

  10. React学习笔记-1-什么是react,react环境搭建以及第一个react实例

    什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...