-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathFibFrog.java
36 lines (34 loc) · 919 Bytes
/
FibFrog.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
29
30
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FibFrog {
public int solution(int[] A) {
List<Integer> steps = new ArrayList<Integer>();
steps.add(1);
steps.add(1);
while (true) {
int nextStep = steps.get(steps.size() - 2)
+ steps.get(steps.size() - 1);
if (nextStep > A.length + 1) {
break;
}
steps.add(nextStep);
}
int[] minJumpNums = new int[A.length + 2];
Arrays.fill(minJumpNums, Integer.MAX_VALUE);
minJumpNums[0] = 0;
for (int i = 0; i < minJumpNums.length; i++) {
if (i == 0 || i == minJumpNums.length - 1 || A[i - 1] == 1) {
for (int step : steps) {
if (step > i) {
break;
}
minJumpNums[i] = (int) Math.min(minJumpNums[i],
(long) minJumpNums[i - step] + 1);
}
}
}
return minJumpNums[minJumpNums.length - 1] == Integer.MAX_VALUE ? -1
: minJumpNums[minJumpNums.length - 1];
}
}