1.题目描述

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.解法分析

首先判断能不能到达最后一个元素。如果能,接着分析。这里有一个动态规划的解法,不过比较耗时,思路是这样的,设置一个数组dp,dp长度与源数组长度一致,为n.其中dp[i]表示到达数组第i个元素的最短步数,那么dp[i]只跟i之前一步能够到达i的位置有关。于是有了下面的代码,小数据集直接就AC了,大数据集却卡住了,极端情况下这个算法的复杂度是O(N2),但是侥幸心理让我还是写了这个解法,但是还是没过,悲催,先记录一下吧。

class Solution {

public:

    int jump(int A[], int n) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        if(n<2)return 0;

        set<int>onestep;

        vector<int> dp;

        dp.assign(n,0);

        

        set<int>::iterator iter;

        for(int i=1;i<n;++i)

        {

            for(iter=onestep.begin();iter!=onestep.end();++iter)

            {

                if((A[*iter]+*iter)<i)onestep.erase(*iter);

            }

            

            if((i-1+A[i-1])>=i)onestep.insert(i-1);

            

            int minStep=n;

            for(iter=onestep.begin();iter!=onestep.end();++iter)

            {

                if(dp[*iter]<minStep)

                {

                    minStep=dp[*iter];

                }

            }

            

            dp[i]=minStep+1;

        }

        

        return dp[n-1];

        

    }

};

然后在网上发现了这么个解法,感觉豁然开朗,我一开始就被动态规划迷住了双眼,这个解法反其道而行之,很妙,思路是这样的,假设我们现在已经知道了再ret步之内能够到达的最远距离last,那么从当前位置到last,我们逐一计算它们一步之内能到达的位置,如果该位置大于last,且大于curr,那么ret+1步之内能到达的最远位置更新为这个值,继续保存在curr之中,一旦我们遍历过了last,那么说明我们需要ret+1步才能到达了,将last设置为curr.重复刚才的判断直至结束。具体的算法很简单,如下:

/*

 * We use "last" to keep track of the maximum distance that has been reached

 * by using the minimum steps "ret", whereas "curr" is the maximum distance

 * that can be reached by using "ret+1" steps. Thus,

 * curr = max(i+A[i]) where 0 <= i <= last.

 */

class Solution {

public:

    int jump(int A[], int n) {

        int ret = 0;

        int last = 0;

        int curr = 0;

        for (int i = 0; i < n; ++i) {

            if (i > last) {

                last = curr;

                ++ret;

            }

            curr = max(curr, i+A[i]);

        }

 

        return ret;

    }

};

 

总结,第一反应的算法不是好算法,要仔细想想,其实这个思路跟jump game那个差不多,只是思路更隐蔽。

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(贪婪算法)

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

  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. Hopcroft-Karp模板学习小结

    最开始是因为做了一个题目接触到这个算法的,但是对于这个算法很多资料都只说了大概的方法: 首先从所有X的未盖点进行BFS,BFS之后对每个X节点和Y节点维护距离标号,如果Y节点是未盖点那么就找到了一条最 ...

  2. javascript中 的 + RegExp['\x241'] 怎么理解

    \x24是十六进制转义符,16*2+4=36,ASCII码36代表的正是“$”符号(可以查ASCII码表),十六进制转义符的一般形式是'\xhh',h是0-9或A-F内的一个.$1是javascrip ...

  3. Android电源管理-休眠简要分析

    一.开篇 1.Linux 描述的电源状态 - On(on)                                                 S0 -  Working - Standb ...

  4. java-基础练习题1

    /** 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,* 假如兔子都不死,问每个月的兔子总数为多少?* 1.程序分析: 兔子的规律为数列1, ...

  5. HDFS的体系结构和操作

    1.对hdfs操作的命令格式是hadoop fs 1.1 -ls <path> 表示对hdfs下一级目录的查看 1.2 -lsr <path> 表示对hdfs目录的递归查看 1 ...

  6. Python3 学习第九弹: 模块学习二之文件管理模块

    os模块 提供访问操作系统的接口 1> name 获得当前操作系统 其中 'nt' 是 windows 'posix' 是 linux 2> environ 获得当前系统的环境变量的字典, ...

  7. 记一次高级java工程师职位的面试

    阿里在业内做java方面的有关开发可谓是一流的.它给我的第一印象,就是办事效率很高. 周日简历发过去,周一电话就打过来了,接到电话后,面试官很客气,问现在方面吗,我说现在在上班,有点忙,然后和面试官约 ...

  8. ios多手势事件

    开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollVi ...

  9. Java之网络编程笔记

    网络通讯要素: 1.IP地址 IP地址:用于标记一台计算机的身份证. IP地址由网络地址(确定网络)和主机地址(网络中的主机)组成. IP地址分为A类地址.B类地址.C类地址(常用).D类地址.E类地 ...

  10. BZOJ 1415 聪聪和可可

    f[i][j]表示i点追j点的期望步数... 这题必须spfa不能bfs. 且复杂度不会炸(仅1000条边) #include<iostream> #include<cstdio&g ...