题目:

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

思路:

第一种思路是利用迭代的思路来计算最小跳数,但是时间复杂度比较大;第二种思路是反过来想,要达到最后一条,倒数第二条至少应该到哪个位置,以此类推直到我们倒推到第一位时便可知最小跳数;第三种思路是用动态规划DP的观点来实现。DP[i]代表到达i的最小跳数,显然DP是一个递增的数组。每次循环只需要尽量找到最小的DP[k],使其满足k+A[k]>=n。

 

代码:

思路一:迭代(时间复杂度不满足要求)

  1. class Solution {
  2. public:
  3. int jump(int A[], int n) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. int* v = new int[1];
  7. v[0] = INT_MAX;
  8. jumpRepeat(A, 0, n-1, 0, v);
  9. if(v[0] == INT_MAX)
  10. {
  11. return 0;
  12. }
  13. return v[0];
  14. }
  15. void jumpRepeat(int A[], int i, int m, int n,int* v)
  16. {
  17. if(i >= m)
  18. {
  19. if(v[0] > n)
  20. {
  21. v[0] = n;
  22. }
  23. return;
  24. }
  25. if(A[i] == 0)
  26. {
  27. return;
  28. }
  29. else
  30. {
  31. for(int j = 1; j <= A[i]; j++)
  32. {
  33. jumpRepeat(A, i + j, m, n+1, v);
  34. }
  35. }
  36. }
  37. };
 
 
思路二:倒推
  1. class Solution {
  2. public:
  3. int jump(int A[], int n) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. int pre = 0;
  7. int cur = n - 1;
  8. int count = 0;
  9. while(true)
  10. {
  11. if(pre == cur)
  12. {
  13. return 0;
  14. }
  15. count++;
  16. pre = cur;
  17. for(int i = n - 2; i >= 0; i--)
  18. {
  19. if(i + A[i] >= pre)
  20. {
  21. if(cur > i)
  22. {
  23. cur = i;
  24. }
  25. }
  26. }
  27. if(cur == 0)
  28. {
  29. return count;
  30. }
  31. };
  32. }
  33. };

思路三:动态规划

  1. class Solution {
  2. public:
  3. int* dp;
  4. int jump(int A[], int n) {
  5. if(n==0)
  6. {
  7. return INT_MAX;
  8. }
  9. dp = new int[n];
  10. dp[0] = 0;
  11. for(int i=1;i<n;i++)
  12. {
  13. dp[i] = INT_MAX;
  14. }
  15. for(int i=1;i<n;i++)
  16. {
  17. for(int j=0;j<i;j++)
  18. {
  19. if(j+A[j]>=i)
  20. {
  21. int tmp = dp[j]+1;
  22. if(tmp < dp[i])
  23. {
  24. dp[i] = tmp;
  25. break;
  26. }
  27. }
  28. }
  29. }
  30. return dp[n-1];
  31. }
  32. };

【To Read】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

    1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...

  8. leetcode Jump Game II python

    @link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...

  9. [Leetcode] jump game ii 跳跃游戏

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

随机推荐

  1. 建造者模式(Builder)(生成器模式)(框架化)

    建造者模式将一个复杂对象的构建与其表示分离. 将复杂对象进行框架化,将同类的对象编造进同一个制造流程.同类·对象会有一样的框架. 而由于各部分的实现细节有所不同,所生产出来的产品会有所不同.从而有不同 ...

  2. java代码乱序问题

    java两个线程互相访问的时候并不能按照你的思路运行,因为执行语句可能有前后快慢之分,比如a=1和flag=true.下面线程B访问的时候 这两个赋值语句不一定按顺序执行 产生这种原因是因为指令重排序 ...

  3. python 当文件目录不存在时,如何自动创建

    import os if not os.path.exists('foldername'): os.mkdir('foldername')

  4. mysql建表设置格式

    建表时必须设置字段编码格式为COLLATE utf8_bin,表示查询时该字段内容区分大小写,如果不需要区分大小写,可以设置为COLLATE utf8_ genera_ci,表示忽略大小写

  5. Laravel使用EasyWechat 进行微信支付

    微信支付和EasyWeChat这个包都是巨坑, 文档写的稀烂, 记录下防止以后又重复踩坑: 安装教程在这: https://www.jianshu.com/p/82d688e1fd2a

  6. processlist

    ###################### 当前会话的线程id,也就是会话id select connection_id(); ########################### select ...

  7. git cherry命令来比较两个分支的不同

    git cherry 命令使用 1. 两个参数的情况 git cherry -v origin/master asa 比较本地的asa分支和远程master的差别 git cherry -v mast ...

  8. switch...case...之替换方案一

    很多时候,当switch中有N个分支,且分支数已达10+,每个分支都是一个不小的方法体,那我们是不是应该考虑换一种方式来实现这个分支. 而我目前所能想到的是会用到如下几种方法. 1.Action 2. ...

  9. 在VUE中使用Echarts

    第一步:下载echarts npm install echarts --save 第二步:在项目中main.js引入 import echarts from 'echarts' Vue.prototy ...

  10. ubuntu 下安装 YouCompleteMe

    1.安装用到的软件包 sudo apt-get install vim sudo apt-get install git sudo apt-get install subversion sudo ap ...