-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSolution.java
More file actions
51 lines (44 loc) · 1.18 KB
/
Solution.java
File metadata and controls
51 lines (44 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package JumpGame;
/**
* User: Danyang
* Date: 1/17/2015
* Time: 23:32
*
* 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.
*/
public class Solution {
/**
* DP Greedy
* f[i] the max length at index i can jump
* f[i] = max(A[i]+i, f[i-1])
*
* Notice:
* 1. condition for above equation
* 2. don't use dummy
* @param A
* @return
*/
public boolean canJump(int[] A) {
if(A.length==1)
return A[0]>=0;
int [] f = new int[A.length];
f[0] = A[0];
for(int i=1; i<A.length; i++) {
if(f[i-1]<i)
return false;
f[i] = Math.max(A[i]+i, f[i-1]);
if(f[i]>=A.length-1)
return true;
}
return false;
}
public static void main(String[] args) {
boolean ret = new Solution().canJump(new int[]{1, 0, 2});
assert ret;
}
}