-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.java
More file actions
59 lines (40 loc) · 1.06 KB
/
JumpGame.java
File metadata and controls
59 lines (40 loc) · 1.06 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
57
58
59
import java.util.ArrayList;
public class JumpGame {
public static void main(String[] args){
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(new Integer(1));
System.out.println(getMinStep(al,0,21));
}
public static int getMinStep(ArrayList<Integer> al, int cur, int end){
if (cur==end){
return al.size()-1;
}
if (cur>end){
return end;
}
int steps = al.size()-1;
int presteps = al.get(steps).intValue();
int plus1 = end;
int equal = end;
int minus1 = end;
al.add(new Integer(presteps+1));
plus1 = getMinStep(al,cur+presteps+1,end);
al.remove(steps+1);
if (plus1<end) return plus1;
if(presteps<=0) equal= end;
else{
al.add(new Integer(presteps));
equal = getMinStep(al,cur+presteps,end);
al.remove(steps+1);
}
if(equal<end) return equal;
if(presteps<=1) minus1 = end;
else{
al.add(new Integer(presteps-1));
minus1 = getMinStep(al,cur+presteps-1,end);
al.remove(steps+1);
}
if(minus1<end) return minus1;
return Math.min(plus1, Math.min(equal, minus1));
}
}