-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrogJump.java
28 lines (27 loc) · 959 Bytes
/
FrogJump.java
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
public class Solution {
public boolean canCross(int[] stones) {
int[] steps = new int[stones.length];
steps[0] = 0;
HashMap<String,Boolean> memo = new HashMap<>();
return help(stones,steps,0, memo);
}
private boolean help(int[]stones, int[] steps,int cur,HashMap<String, Boolean>memo){
if(cur == stones.length-1){
return true;
}
String key = String.valueOf(cur)+ String.valueOf(steps[cur]);
if(memo.containsKey(key)){
return memo.get(key);
}
boolean res = false;
for(int i=steps[cur]+1;i>=0 && i>=steps[cur]-1;i--){
for(int j = cur+1;j<stones.length;j++)
if(stones[cur]+ i ==stones[j]){
steps[j] = i;
res |= help(stones,steps,j, memo);
memo.put(String.valueOf(j)+String.valueOf(i),res);
}
}
return res;
}
}