题目:

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. (视频分辨率介绍)混淆的概念:SIF与CIF、4CIF与D1

    http://www.microjie.com/index.php/professional-knowledge/82-standards-parterns/26-profession-knowled ...

  2. KeyOnlyFilter(2)

    主要用来过滤剩下行键计数一类 KeyOnlyFilter 官方API解释如下: A filter that will only return the key component of each KV ...

  3. SVN 提交时文件锁定 svn: E155004: '' is already locked

    1.先安装TortoiseSVN TortoiseSVN安装成功后,找到工作路径下的项目右键 TortoiseSVN --> Clean up... --> Break locks 勾选上 ...

  4. Leetcode563.Binary Tree Tilt二叉树的坡度

    给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点的的坡度是0. 整个树的坡度就是其所有节点的坡度之和. 示例: 输入: 1 / ...

  5. R语言中如何使用最小二乘法

    R语言中如何使用最小二乘法 这里只是介绍下R语言中如何使用最小二乘法解决一次函数的线性回归问题.         代码如下: > x<-c(6.19,2.51,7.29,7.01,5.7, ...

  6. 【JZOJ5233】【GDOI模拟8.5】概率博弈 树形dp+期望

    题面 小A和小B在玩游戏.这个游戏是这样的: 有一棵n个点的以1为根的有根树,叶子有权值.假设有m个叶子,那么树上每个叶子的权值序列就是一个1->m 的排列. 一开始在1号点有一颗棋子.两人轮流 ...

  7. Eviews9.0---软件安装

    EViews是Econometrics Views的缩写,直译为计量经济学观察,通常称为计量经济学软件包.它的本意是对社会经济关系与经济活动的数量规律,采用计量经济学方法与技术进行“观察”.计量经济学 ...

  8. JS 获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  9. oracle-DML-2

    1.update 语句 update  table set  [column,column......] where  column ='' 示例: update   customers set   ...

  10. 原型模式 —— Java的赋值、浅克隆和深度克隆的区别

    赋值 直接  = ,克隆 clone 假如说你想复制一个简单变量.很简单: int a= 5; int b= a; b = 6; 这样 a == 5, b == 6 不仅仅是int类型,其它七种原始数 ...