LeetCode: Jump Game II 解题报告

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

SOLUTION 1:

参考:http://blog.csdn.net/fightforyourdream/article/details/14517453

我们可以使用贪心法来解决这个问题。

从后往前思考问题。以 des = len - 1反向思考。思考能到达它的最远的距离是多少。

例子:

2 3 1 1 4

   i = 1

上例子中index i = 1是达到4的最远的距离。这时index i = 1 到4是最后一步的最优解,因为其它的解,如果跳到index = 2, 3 到达4为最后一步,那么倒数第二步既然可以到达 index = 2, 3 也可以到达index = 1,所以跳到index = 1步数会是一样的。所以其它的解最好的情况也就是与从index = 1跳过去一样而已。

而前面的点因为距离限制 ,有可能只能跳到index = 1,而不可以跳到index = 2, 3.所以 将倒数第二步设置在index = 1可以得到最多的解。

 1 package Algorithms.greedy;
 2 
 3 public class Jump {
 4     public static void main(String[] strs) {
 5         int[] A = {2, 3, 1, 1, 4};
 6         System.out.println(jump(A));
 7     }
 8     
 9     public static int jump(int[] A) {
10         if (A == null || A.length == 0) {
11             return 0;
12         }
13 
14         int len = A.length;
15 
16         int sum = 0;
17 
18         int des = len - 1;
19         while (des > 0) { // destination index
20             for (int i = 0; i < des; i++) { // 不断向前移动dest
21                 if (A[i] + i >= des) { // 说明从i位置能1步到达dest的位置
22                     sum++;
23                     des = i; // 更新dest位置,下一步就是计算要几步能调到当前i的位置
24                     //break; // 没必要再继续找,因为越早找到的i肯定越靠前,说明这一跳的距离越远
25                              // 这一行可以去掉, des = i,不符合for的条件,会自动break.
26                     System.out.println("sum:" + sum);
27                     System.out.println("des:" + des);
28                 }
29             }
30         }
31 
32         return sum;
33     }
34 }
View Code

 SOLUTION 2:(2015.1.13 redo)

Leetcode增强test case之后,前面的算法不能通过,感谢http://fisherlei.blogspot.com/2012/12/leetcode-jump-ii.html的灵感:

[解题思路]
二指针问题,最大覆盖区间。
从左往右扫描,维护一个覆盖区间,每扫过一个元素,就重新计算覆盖区间的边界。比如,开始时区间[start, end], 遍历A数组的过程中,不断计算A[i]+i最大值(即从i坐标开始最大的覆盖坐标),并设置这个最大覆盖坐标为新的end边界。而新的start边界则为原end+1。不断循环,直到end> n.

 1 // solution 2: one pass greedy.
 2     public int jump(int[] A) {
 3         if (A == null || A.length == 0) {
 4             return 0;    
 5         }
 6         
 7         // bug:
 8         /*
 9         Input:    [1]
10         Output:    1
11         Expected:    0
12         */
13         if (A.length == 1) {
14             return 0;
15         }
16         
17         int len = A.length;
18         
19         int start = 0;
20         int end = 0;
21         
22         int steps = 0;
23         while (end < len - 1) {
24             int max = 0;
25             steps++;
26             for (int i = start; i <= end; i++) {
27                 max = Math.max(max, i + A[i]);
28                 
29                 if (max >= len - 1) {
30                     return steps;
31                 }
32             }
33             
34             start = end + 1;
35             end = max;
36             
37             if (start > end) {
38                 break;
39             }
40         }
41         
42         return Integer.MAX_VALUE;
43     }
View Code

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/greedy/Jump.java

原文地址:https://www.cnblogs.com/yuzhangcmu/p/4148858.html