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

  Greedy:  keep the current maximum reach distance, and the number of steps to reach this current maximum distances, and keep another variable to record the next maximum reachable distance, which cost the current steps plus 1. The key idea behind is that, all the positions before the maximum reachable distance would be able to be reached! Then we linear scan the array to keep updating the current maximum and the next maximum as well as the number of steps. We can achieve the linear time algorithm

class Solution {
public:
int jump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int steps = ;
int currMax = ;
int nextMax = ;
for(int i = ; i< n; i++)
{
if(i > currMax)
{
currMax = nextMax;
steps++;
} nextMax = nextMax > i+A[i] ? nextMax : i+A[i] ; }
return steps ;
}
};

LeetCode_Jump Game II的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  3. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

  4. 统计分析中Type I Error与Type II Error的区别

    统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...

  5. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  6. [LeetCode] Guess Number Higher or Lower II 猜数字大小之二

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  7. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  8. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  9. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. 安装Oracle10g on RedHat as 4 64bit(摘)

    一.安装前的配置 1.修改RH版本 vi /etc/redhat-release Red Hat Enterprise Linux AS release 3 (Taroon Update 3) 2. ...

  2. RMAN学习笔记

    RMAN:如果RMAN连接一个远程数据库,格式:RMAN>rman target sys/jxsrpv@test 1.列出备份信息,所有的备份信息 RMAN>list backup of ...

  3. Codeforces 571B Minimization

    http://codeforces.com/problemset/problem/571/B 给出一个序列,可以任意调整序列的顺序,使得给出的式子的值最小 思路:我们可以把序列分解,变成k条链,n%k ...

  4. 董事长、总裁与CEO的区别与实质

    自从信息产业兴起以来,尤其是网络股泡沫产生以来,“CEO”在中国骤然成为一个流行词汇.总经理和总裁们纷纷改称CEO,这个缩写词比它的中译版“首席执行官”更简洁,在中国人心目中更有神圣感,于是便出现了今 ...

  5. Box.net获5000万美元投资(SAAS博士)

    9月29日,据国外媒体报道,“云”存储服务商Box.net已经从Salesforce等投资者获得新一轮融资5000万美元. Box.net过去的投资者也参与了新一轮的融资.最终,在CRM巨人Sales ...

  6. eclipse打开文件位置Open Explorer 插件

    ,OpenExplorer插件可以满足这个功能,可以到https://github.com/samsonw/OpenExplorer/downloads下载最新版本,将jar包放到eclipse的pl ...

  7. mybatis源代码分析:mybatis延迟加载机制改进

    在上一篇博客<mybatis源代码分析:深入了解mybatis延迟加载机制>讲诉了mybatis延迟加载的具体机制及实现原理. 可以看出,如果查询结果对象中有一个属性是需要延迟加载的,那整 ...

  8. PHP与C++的不同

    由于工作需要,需要学习一下PHP,由于3年的C++背景,在刚开始学习PHP的过程中,有些不习惯,经过一段时间的学习,总结了一些PHP与C++的不同. 1.应用场景 在谈两种语言不同的时候,首先需要了解 ...

  9. jdbc连接数据库工具类

    import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import ja ...

  10. EasyUI 两个日期比较

    两个日期进行比较,后一个日期不能早于晚一个日期.需要自己去扩展validatebox的方法 1.对比2个密码是否相同 $.extend($.fn.validatebox.defaults.rules, ...