-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathJumpGame.java
More file actions
56 lines (46 loc) · 1.52 KB
/
JumpGame.java
File metadata and controls
56 lines (46 loc) · 1.52 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
52
53
54
55
56
// recursive with cache, could not pass the large judge
import java.util.Hashtable;
public class Solution {
public boolean canJump(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
if(A.length<=1) return true;
int lastIndex = A.length-1;
Hashtable<Integer[], Boolean> hash = new Hashtable<Integer[], Boolean>();
return canJump(A,0,lastIndex,hash);
}
public boolean canJump(int[] A, int start, int last, Hashtable<Integer[], Boolean> hash){
if(last==start) return true;
Integer[] temp = {start,last};
if(!hash.containsKey(temp)){
hash.put(temp,false);
for(int i=start; i<last; i++){
if(i+A[i]>=last && canJump(A,start,i,hash)){
hash.put(temp,true);
break;
}
}
}
return hash.get(temp);
}
}
// Easy way to solve PASS BOTH TEST
public class Solution {
public boolean canJump(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
if(A.length<=1) return true;
int last = A.length -1;
int ll;
while(last>0){
ll = last;
for(int i=0; i<last; i++){
if(i+A[i]>=last)
last = i;
}
if(ll == last)
return false;
}
return true;
}
}