- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1414.java
41 lines (39 loc) · 1.25 KB
/
_1414.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
37
38
39
40
41
packagecom.fishercoder.solutions.secondthousand;
importjava.util.PriorityQueue;
publicclass_1414 {
publicstaticclassSolution1 {
/*
* My completely original solution, heap is not really necessary, a regular array/list works out as well.
*/
publicintfindMinFibonacciNumbers(intk) {
PriorityQueue<Integer> maxHeap = newPriorityQueue<>((a, b) -> b - a);
intone = 1;
inttwo = 1;
maxHeap.offer(one);
maxHeap.offer(two);
intthree = one + two;
while (three <= k) {
maxHeap.offer(three);
inttmp = two;
two = three;
one = tmp;
three = one + two;
}
intminNumbers = 0;
while (k > 0) {
if (maxHeap.contains(k)) {
minNumbers++;
returnminNumbers;
} else {
while (maxHeap.peek() > k) {
maxHeap.poll();
}
Integermax = maxHeap.poll();
k -= max;
minNumbers++;
}
}
returnminNumbers;
}
}
}