zoukankan      html  css  js  c++  java
  • [LintCode] 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.

    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 steps to the last index.)

    Solution 1. Recursion without memoization

    Similarly with Jump Game, we can use solve this problem recursively with the following formula.

    f(n) = min(1 + f(i)) for i satisfies in range [0, n - 1], i + A[i] >= n, f(i) is reachable from A[0];

    For f(i) that can't be reached from A[0], it does not update the min.

     1 public class Solution {
     2     private int min;
     3     public int jump(int[] A) {
     4         if(A == null || A.length == 0){
     5             return Integer.MAX_VALUE;
     6         }
     7         min = Integer.MAX_VALUE;
     8         helper(A, A.length - 1, 0);
     9         return min;
    10     }
    11     private void helper(int[] A, int idx, int jumps){
    12         if(idx == 0){
    13             min = Math.min(min, jumps);
    14             return;
    15         }
    16         for(int i = 0; i < idx; i++){
    17             if(i + A[i] >= idx){
    18                 helper(A, i, jumps + 1);            
    19             }
    20         }
    21     }
    22 }

    Solution 2. Top Down Recursion with Memoization, O(n^2) runtime, O(n) space

     1 public class Solution {
     2     private int[] T;
     3     public int jump(int[] A) {
     4         if(A == null || A.length == 0){
     5             return Integer.MAX_VALUE;
     6         }
     7         T = new int[A.length];
     8         T[0] = 0;
     9         for(int i = 1; i < A.length; i++){
    10             T[i] = Integer.MAX_VALUE;
    11         }
    12         return helper(A, A.length - 1);
    13     }
    14     private int helper(int[] A, int idx){
    15         if(T[idx] != Integer.MAX_VALUE){
    16             return T[idx];
    17         }
    18         for(int i = 0; i < idx; i++){
    19             if(i + A[i] >= idx){
    20                 int ret = helper(A, i);
    21                 if(ret != Integer.MAX_VALUE){
    22                     T[idx] = Math.min(T[idx], ret + 1);
    23                     break;
    24                 }
    25             }
    26         }
    27         return T[idx];
    28     }
    29 }

    Solution 3. Bottom Up Dynamic Programming

     1 public class Solution {
     2     public int jump(int[] A) {
     3         if(A == null || A.length == 0){
     4             return Integer.MAX_VALUE;
     5         }
     6         int[] steps = new int[A.length];
     7         steps[0] = 0;
     8         for (int i = 1; i < A.length; i++) {
     9             steps[i] = Integer.MAX_VALUE;
    10         }
    11         for (int i = 1; i < A.length; i++) {
    12             for (int j = 0; j < i; j++) {
    13                 if (steps[j] != Integer.MAX_VALUE && j + A[j] >= i) {
    14                     steps[i] = Math.min(steps[i], steps[j] + 1);
    15                     break;
    16                 }
    17             }
    18         }
    19         return steps[A.length - 1];
    20     }
    21 }

    In both solution 2 and 3, the highlighted break statement is actually an optimization upon the dynamic programming solution. 

    It uses a greedy principle: given an index i and we try to find the min jumps needed to get from index 0 to i. Our search index 

    is from 0 to i - 1. If we can jump directly from 0 to i with 1 jump, then we should simply stop searching the rest 1 to i - 1.  Why?

    Say from 1 to i - 1, we find another index j that we can jump from j to i with 1 jump, then we need at least another 1 extra jump 

    to get from 0 to j, which is obviously less optimal than jumping from 0 to i. 

    This argument only works if we search from left to right. If we search from right to left, then we can't skip any search.

    Solution 4. Greedy Algorithm, O(n) runtime.

    Stay tuned...

    Related Problems 

    Jump Game

    Frog Jump

  • 相关阅读:
    函数传参的方式
    统计一个文本中单词频次最高的 10 个单词?
    统计一个文本中单词频次最高的 10 个单词?
    python每日练习0801
    关键字驱动小练习
    Fiddler抓包从入门到不放弃
    pycharm2019.1.3激活方法--激活码可以用到2089年
    Selenium+Python调Chrome浏览器时报Traceback (most recent call last): File "C:/Users/EDZ/Desktop/selenium_demo/demo001.py", line 12, in <module>
    JMeter服务端压测,人人都会的小白版本
    XShell命令大全
  • 原文地址:https://www.cnblogs.com/lz87/p/7057588.html
Copyright © 2011-2022 走看看