zoukankan      html  css  js  c++  java
  • 【To Read】LeetCode | Jump Game II(转载)

    题目:

    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。
     

    代码:

    思路一:迭代(时间复杂度不满足要求)
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    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.           
    9.         jumpRepeat(A, 0, n-1, 0, v);  
    10.           
    11.         if(v[0] == INT_MAX)  
    12.         {  
    13.             return 0;  
    14.         }  
    15.           
    16.         return v[0];  
    17.     }  
    18.       
    19.     void jumpRepeat(int A[], int i, int m, int n,int* v)  
    20.     {  
    21.         if(i >= m)  
    22.         {  
    23.             if(v[0] > n)  
    24.             {  
    25.                 v[0] = n;  
    26.             }  
    27.             return;  
    28.         }  
    29.         if(A[i] == 0)  
    30.         {  
    31.             return;  
    32.         }  
    33.         else  
    34.         {  
    35.             for(int j = 1; j <= A[i]; j++)  
    36.             {  
    37.                 jumpRepeat(A, i + j, m, n+1, v);  
    38.             }  
    39.         }  
    40.     }  
    41. };  


     
     
    思路二:倒推
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    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.      
    34.     }  
    35. };  

    思路三:动态规划
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    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.           
    31.         return dp[n-1];  
    32.     }  
    33. };  
  • 相关阅读:
    Excelpackage的简单使用(导出用户信息并保存)
    set nocount on/off的作用,可配合存储过程使用
    在sql中case子句的两种形式
    C#开发微信门户及应用(1)--开始使用微信接口(转)
    张建总的一封信
    Jquery在线工具
    《EnterLib PIAB深入剖析》系列博文汇总 (转)
    微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART1——为什么要使用Unity? (转)
    结合领域驱动设计的SOA分布式软件架构 (转)
    ENode框架旨在帮助我们开发ddd,cqrs、eda和事件采购风格的应用程序。(netfocus 汤雪华)
  • 原文地址:https://www.cnblogs.com/helloWaston/p/4616092.html
Copyright © 2011-2022 走看看