-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibFrog.java
More file actions
67 lines (56 loc) · 1.88 KB
/
FibFrog.java
File metadata and controls
67 lines (56 loc) · 1.88 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
60
61
62
63
64
65
66
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
//BFS method
public class Jump {
int pos;
int move;
public Jump(int pos, int move) {
this.pos = pos;
this.move = move;
}
}
private List <Integer> fibArr(int n) {
//find the Fibonacci sequence the the frog can jump with N
List <Integer> fibs = new ArrayList <> ();
fibs.add(1);
fibs.add(1);
while (fibs.get(fibs.size() - 1) + fibs.get(fibs.size() - 2) <= n) {
fibs.add(fibs.get(fibs.size() - 1) + fibs.get(fibs.size() - 2));
}
return fibs;
}
public int solution(int[] A) {
// write your code in Java SE 8
int N = A.length;
List <Integer> fibJmp = fibArr(N);
//BFS init
Queue <Jump> pos = new LinkedList <Jump> ();
boolean[] visited = new boolean[N];
if (N<=2){
return 1;
}
for (int i = 0; i < fibJmp.size(); i++) {
int initPos = fibJmp.get(i) - 1;
if (A[initPos] == 0)
continue;
pos.add(new Jump(initPos, 1));
visited[initPos] = true;
}
while (!pos.isEmpty()) { //have node
Jump jump = pos.remove();
for (int j = fibJmp.size() - 1; j >= 0; j--) {
int nextPos = jump.pos + fibJmp.get(j); //greedy attempt
if (nextPos == N)
return jump.move + 1; //min result of BFS
else if (nextPos < N && A[nextPos] == 1 && !visited[nextPos]) {
pos.add(new Jump(nextPos, jump.move + 1));
visited[nextPos] = true;
}
}
}
return -1;
}
}