题目:

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. [Day5] Nginx 变量

    一. Nginx中的变量原理 提供变量的模块和使用变量的模块 nginx启动,提供变量的模块会在一个回调函数中定义新的变量名和解析出变量的方法. 请求来了以后,使用变量的模块会根据变量名,去调用解析变 ...

  2. Leetcode559.Maximum Depth of N-ary TreeN叉树的最大深度

    给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 说明: 树的深度不会超过 1000. 树的节点总不会超过 5000. class Solution { ...

  3. PHP CURL header 设置HOST主机头进行访问并 POST提交數據

    $host = array("Host: act.qzone.qq.com");// 域名不帶http://$data = array(            'aa' => ...

  4. 原生JS上传,实现预览并且兼容大部分IE

    // 前提条件: ie浏览器模式下,用户要允许ie默认的加载项:以下兼容ie的方法才会生效 // 图片上传预览 IE是用了滤镜 function previewImage(file) { var MA ...

  5. 区块链、云计算、大数据、人工智能、FinTech带来的挑战与机遇,中国技术开放日上海站精彩回顾

    区块链.云计算.大数据.人工智能.FinTech带来的挑战与机遇,中国技术开放日上海站精彩回顾 | 作者 韩婷 发布于 2016年12月26日. 估计阅读时间: 不到一分钟 | 欲知区块链.VR.Te ...

  6. OpenLayers修改要素

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 3D hover文字特效

    body { font-family: 'Source Sans Pro', Arial, sans-serif; background: #becccc; text-transform: upper ...

  8. htmlunit学习之java.lang.NoSuchMethodError: com.gargoylesoftware.htmlunit.WebClient.getOptions()Lcom/gargoylesoftware/htmlunit/WebClientOptions;

    运行到这里就报错 java.lang.NoSuchMethodError: com.gargoylesoftware.htmlunit.WebClient.getOptions()Lcom/gargo ...

  9. JQ效果 透明图片覆盖动画

    效果图呈上 先说思路 1,一个固定的框架,有两张图片,一张是狗狗的,一张是练习方式,想把做好的练习方式隐藏 2,效果上想要从下面滑动出来,所以透明框定位在下面 3,整理需要的东西,缓慢升起需要动画效果 ...

  10. centos7默认安装没有连接网络

    1.显示所有连接 #nmcli con show 2.连接网络 #nmcli con up ens33 这个ens33是通过第一步查到的 /etc/sysconfig/network-scripts目 ...