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. R之pryr

    1. Pryr安装 由于项目pryr,还没有发布到CRAN,仅支持从github安装.要使用devtools包来通过github来安装,在https://github.com/hadley/pryr中 ...

  2. MySQL 跳过同步错误方法

    最近MySQL 遇到了同步问题,现整理一下常遇到的错误的解决方法,备用. 方法一:手动设置动态参数 sql_slave_skip_counter 我常用的脚本: stop slave sql_thre ...

  3. 红黑树、B(+)树、跳表、AVL等数据结构,应用场景及分析,以及一些英文缩写

    在网上学习了一些材料. 这一篇:https://www.zhihu.com/question/30527705 AVL树:最早的平衡二叉树之一.应用相对其他数据结构比较少.windows对进程地址空间 ...

  4. 从svn检出的项目如何编译

    从svn检出的项目如何编译   svn检查项目后,不能构建编译 工程右键,bulid path -->No actions available   问题:svn检查项目后,发现没有class文件 ...

  5. KEYUSE

    typedef struct keyuse_t { TABLE *table; Item *val; /**< or value if no field */ table_map used_ta ...

  6. (转)ios获取设备系统信息

    UIDevice *device_=[[UIDevice alloc] init]; NSLog(@"设备所有者的名称--%@",device_.name); NSLog(@&qu ...

  7. Linux 查找文件方法

    1) find -name httpd.conf 2) find /etc -name "*repo" 详情查找命令-> http://www.yesky.com/210/1 ...

  8. 08day2

    引爆炸弹 贪心 [问题描述] 有 n 个炸弹,有些炸弹牵了一根单向引线(也就是说引线只有在这一端能被炸弹点燃),只要引爆了这个炸弹,用引线连接的下一个炸弹也会爆炸.每个炸弹还有个得分,当这个炸弹被引爆 ...

  9. 【C#学习笔记】保存文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. php的session.serialize_handler

    php里面的session.serialize_handler用来设置php的session的序列化方式,默认值为php,及使用php的序列化与反序列化. 还有一个值为session.serializ ...