zoukankan      html  css  js  c++  java
  • [Leetcode 100] 130 Jump Game II

    Problem:

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

    Analysis:

    At first thought, this problem can be solved by DP. Starting from the first position, for every reachable position from first position, copmare the current step with the step from first position to get to it and take the min one. Then continue this process at 2, 3, 4, ..... n position, at last the step[n-1] keeps the minimum steps needed to get from start to the end. But this method can't pass the large test case. One possible input is [1000, 999, 998, ....., 1], the conplexity is O(N^2). It's untorable.

    So we have to think more simple ways to solve it. Every time we keep the maximum position we can go until now. And we still use an array to keep steps needed to get each position. But unlike the above method, if A[i]+i is less than maximum, we simply skip it. And if A[i]+i > maximum, we only set those [maximum+1, A[i]+i] positions step to step[i]+1. And if anytime we reach the end of the array, we exit the computing. This method is totally linear. After searching online, I know this method is called greedy method.

    Code:

    DP solution (TLE for large test)

     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 *step = new int[n];
     7         int MAX = (1<<31)-1;
     8         
     9         for (int i=1; i<n; i++)
    10             step[i] = MAX;
    11         step[0] = 0;    
    12         
    13         for (int i=0; i<n; i++) {
    14             for (int j=1; j<=A[i] && (i+j)<n; j++) {
    15                 step[i+j] = min(step[i]+1, step[i+j]);
    16             }
    17         }
    18         
    19         return step[n-1];
    20     }
    21     
    22 private:
    23     int min(int a, int b) {
    24         return (a < b) ? a : b;
    25     }
    26 };
    View Code

    Greedy Solution (48ms for large test)

     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 max = 0;
     7         int *step = new int[n];
     8         
     9         step[0] = 0;
    10         for (int i=0; i<n; i++) {
    11             if (max >= (n-1)) break;
    12 
    13             if (max < (A[i] + i)) {
    14                 for (int j=max+1; j<=A[i]+i && j < n; j++)
    15                     step[j] = step[i]+1;
    16 
    17                 max = A[i] + i;
    18             }
    19         }
    20         
    21         return step[n-1];
    22     }
    23 };
    View Code
  • 相关阅读:
    mybatis typeAlias (别名)说明
    jsp路径
    Jutil 单元测试
    SpringMVC数据绑定
    EL表达式(Exprission language)
    JSTL (标准标签库)
    Introduce Null Object
    Replace Type Code With Class和Replace Type Code With Subclass和Replace Type Code With State/Strategy
    利用QBuffer和QLinkedList做数据块存储队列
    Duplicate Observed Data
  • 原文地址:https://www.cnblogs.com/freeneng/p/3242289.html
Copyright © 2011-2022 走看看