-
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
package com.fishercoder.solutions.secondthousand;
import java.util.PriorityQueue;
public class _1414 {
public static class Solution1 {
/*
* My completely original solution, heap is not really necessary, a regular array/list works out as well.
*/
public int findMinFibonacciNumbers(int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
int one = 1;
int two = 1;
maxHeap.offer(one);
maxHeap.offer(two);
int three = one + two;
while (three <= k) {
maxHeap.offer(three);
int tmp = two;
two = three;
one = tmp;
three = one + two;
}
int minNumbers = 0;
while (k > 0) {
if (maxHeap.contains(k)) {
minNumbers++;
return minNumbers;
} else {
while (maxHeap.peek() > k) {
maxHeap.poll();
}
Integer max = maxHeap.poll();
k -= max;
minNumbers++;
}
}
return minNumbers;
}
}
}