LeetCode: Jump Game Total 解题报告

Jump Game
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.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

Solutions:

1. DP1
每到一个点 i,我们扫描之前所有的点,如果之前某点j本身可达,并且与current 点可达,表示点i是可达的。

返回值:DP数组的最后一个值。

 1 // DP1.
 2     public boolean canJump1(int[] A) {
 3         if (A == null || A.length == 0) {
 4             return false;
 5         }
 6 
 7         int len = A.length;
 8         boolean[] can = new boolean[len];
 9         can[0] = true;
10 
11         for (int i = 1; i < len; i++) {
12             can[i] = false;
13             for (int j = 0; j < i; j++) {
14                 // j can arrive and can jump to i.
15                 if (can[j] && A[j] >= i - j) {
16                     can[i] = true;
17                     break;
18                 }
19             }
20         }
21 
22         return can[len - 1];
23     }
View Code

2. DP2
优化的点1:既然某点可达,肯定前面的点全部是可达的。这个比较好理解。因为你到达i点肯定要经过前面的一个点,这个依次推知可知前面所有的点可达。

所以我们不需要数组来记录结果,只要默认i点前全部可达就行了。

优化点2:如果某点不可达了,直接返回false。不需要再往后计算。

返回值:如果全部扫完仍没有返回false,返回true。

 1 // DP2.
 2     public boolean canJump2(int[] A) {
 3         if (A == null || A.length == 0) {
 4             return false;
 5         }
 6 
 7         int len = A.length;
 8 
 9         for (int i = 1; i < len; i++) {
10             boolean can = false;
11             for (int j = 0; j < i; j++) {
12                 // j can arrive and can jump to i.
13                 if (A[j] >= i - j) {
14                     // 说明i是可达的,置标记位
15                     can = true;
16                     break;
17                 }
18             }
19 
20             // 优化:如果某一步已经到不了了,后面的也不必再计算了.
21             if (!can) {
22                 return false;
23             }
24         }
25 
26         return true;
27     }
View Code

3. 递归
思想是,从前至尾扫描,至第一个距离与本点可达的点j,计算j点是否可达。使用递归计算j

点的可达性。

其实这里还是用到了贪心的思维。在考虑本点是否可达的时候,我们是考虑与本点最远的一个点是否可达。实际上这也make sense。

假设j点可以到达i点,那么后面的点可以不管。

(1)因为如果j点不可达,那么j+1也不可达。如果i不可达,后面的点也可不算。

(2)如果j点可达,并且j点可到达i,那么也没有必要再往后计算了。因为结论已经出来了。

  (3) 如果j点可达,但j不可达i,那么就继续计算。

 1 // 3. DFS.
 2     public static boolean canJump3(int[] A) {
 3         if (A == null || A.length == 0) {
 4             return false;
 5         }
 6 
 7         return canJump(A, A.length - 1);
 8     }
 9 
10     public static boolean canJump(int[] A, int index) {
11         if (index == 0) {
12             return true;
13         }
14 
15         for (int i = 0; i <= index - 1; i++) {
16             if (A[i] >= index - i) {
17                 return canJump(A, i);
18             }
19         }
20 
21         return false;
22     }
View Code

4. 贪心法(2015.1.13 redo)

Leetcode加强了test case,用动规现在是过不了的。我们现在来使用贪心法One pass解决此问题。

维护一个right (表示右边能跳到的最远的点),从左往右扫描,根据当前可跳的步骤不断更新right ,当right到达终点,即可返回true. 若更新完right后,right未

动,并且index = right,而且这时没到达终点,代表我们不可能到达终点了。(当前index的可跳值应该是0)。

 1 // solution 3: one pass.
 2     public boolean canJump(int[] A) {
 3         // 4:42
 4         if (A == null) {
 5             return false;
 6         }
 7         
 8         int len = A.length;
 9 
10         int right = 0;        
11         for (int i = 0; i < A.length; i++) {
12             right = Math.max(right, i + A[i]);
13             if (right == len - 1) {
14                 return true;
15             }
16             
17             if (i == right) {
18                 return false;
19             }
20         }
21         
22         return true;
23     }
View Code

5. GitHub代码

CanJump.java

References:

感谢http://blog.csdn.net/fightforyourdream/article/details/14219049给予的灵感。

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