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. vmware 虚拟机 桥接 设置静态 IP

    最简单的方式: 图形界面下,进入设置IP的地方,设置一个静态IP. 然后再转入命令行继续工作.. 不行就 重启  

  2. SQLServer2008 行转列2

    with a as ( select numb,name,row_number() over( partition by numb order by name desc) rowid from fen ...

  3. Tomcat遇到”Error listenerStart”或”Error filterStart”问题且无详细日志时的log配置.

    昨天部署web应用到Tomcat之后,无法成功启动,并且控制台没有详细的错误信息,顶多就两行提示信息,例如:严重: Error listenerStart严重: Context [/lizongbo] ...

  4. ASP.NET26 个常用性能优化方法

    数据库访问性能优化 数据库的连接和关闭 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器资源. ASP.NET中提供了连接池(Co ...

  5. css中图片的四种地址引用

    URL: CSS中四种引用图片asset的方式:

  6. XManager介绍、安装、使用

    简介 Xmanager是一款小巧.便捷的浏览远端X窗口系统的工具.在工作中经常使用Xmanager来登录远端的Linux系统,在X窗口系统上作图形化的操作.Xmanager可以将PC变成X Windo ...

  7. Oracle® Database Patch 19121551 - Database Patch Set Update 11.2.0.4.4 (Includes CPUOct2014) - 傲游云浏览

    Skip Headers Oracle® Database Patch 19121551 - Database Patch Set Update 11.2.0.4.4 (Includes CPUOct ...

  8. Http进行网络通信

    http使用get的方式进行网络通信: package com.testGet; import java.io.BufferedReader; import java.io.IOException; ...

  9. acdream 1412 2-3Trees (组合+DP)

    题意:2-3树的每个结点(除了叶子外)有2或3个孩子(分支),假设是一个满2-3树,那么给出叶子的数量,求这样的树有多少棵.(注:有2个孩子的结点视为相同,有3个孩子的结点视为相同,比如倒数第2层有4 ...

  10. erl0008 - unicode 和 utf-8之间的关系

    转载:http://blog.jobbole.com/84903/ 原文出处: 卢钧轶   欢迎分享原创到伯乐头条 本文将简述字符集,字符编码的概念.以及在遭遇乱码时的一些常用诊断技巧. 背景:字符集 ...