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. 产经新闻:公交WiFi这次能扛多久

    来源:16WiFi.流量咪  http://www.16wifi.com/18820/mtbd/html/1096904.html 不靠谱也许是成长的烦恼,也可能是本性使然.公交WiFi就给人一种不靠 ...

  2. LoadRunner 获取接口请求响应信息

    Action() { int nHttpRetCode; // 默认最大长度为256,get请求需注意缓存问题,需要根据content-length进行修改 web_set_max_html_para ...

  3. 【09-14】eclipse学习笔记

    eclipse安装class文件反编译插件jadClipse /** 1. 下载JadClipse的jar包 2. 下载Jad反编译器 3. 将JarClipse jar包放到eclipse plug ...

  4. ActiveMQ启动多个broker

    具体步骤如下: 1.把activemq目录下的conf文件复制一份,叫做conf2, 命令: cp -r conf conf2 2.修改conf2目录下的activemq.xml文件 a.修改brok ...

  5. 利用pg_stat_activity进行问题排查

    pg_stat_activity是一个非常有用的视图,可以帮助排查pg的一些问题(如连接数目过多问题).pg_stat_activity每行展示的是一个“process”的相关信息,这里的“proce ...

  6. ASP.NET 系统对象 Request(一)

    Request对象 用来获取客户端在请求一个页面或传送一个Form是提供的所有信息.它包括用户的HTTP变量.能够识别的浏览器.存储客户端的Cookie信息和请求地址等. Request对象是Syst ...

  7. Python_DB_Api

    python DB API 内容 建立连接connection 数据库交互对象cursor 数据库异常类exception 流程 创建connection 获取cursor 执行查询.执行命令.获取数 ...

  8. Codeforces#262_1002

    Codeforces#262_1002 B. Little Dima and Equation time limit per test 1 second memory limit per test 2 ...

  9. Windows下,MySQL root用户忘记密码解决方案

    同时打开2个命令行窗口,并按如下操作: <1>.在第一个“命令行窗口”输入: cd  D:\Program Files\MySQL\MySQL Server 5.5\bin net sto ...

  10. 新语言代码高亮及Windows Live Writer插件开发

    最近在博客园做一些学习笔记.一个是看apple的swift官方书,另外一个是随学校课堂(SICP)学习scheme. 这两种语言都谈不上普及(或者说swift太新).博客园原来的windows liv ...