Skip to content

Commit fac1193

Browse files
add 1414
1 parent bb83f3f commit fac1193

File tree

3 files changed

+70
-1
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+70
-1
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
| 1418 | [Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1418.java) | | Medium | HashTable |
263263
| 1417 | [Reformat The String](https://leetcode.com/problems/reformat-the-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1417.java) | | Easy | String |
264264
| 1415 | [The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1415.java) | | Medium | Backtracking |
265+
| 1414 | [Find the Minimum Number of Fibonacci Numbers Whose Sum Is K](https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1414.java) | | Medium | Array, Greedy |
265266
| 1413 | [Minimum Value to Get Positive Step by Step Sum](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1413.java) | | Easy | Array |
266267
| 1410 | [HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1410.java) | | Medium | String, Stack |
267268
| 1409 | [Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1409.java) | | Medium | Array |
@@ -316,7 +317,7 @@
316317
| 1343 | [Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1343.java) | | Medium | Array |
317318
| 1342 | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1342.java) | | Easy | Bit Manipulation |
318319
| 1341 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1341.java) | | Easy ||
319-
| 1339 | [Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1339.java) | | Medium | DFS, Tree |
320+
| 1339 | [Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1339.java) | | Medium | DFS, Tree |
320321
| 1338 | [Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1338.java) | | Medium ||
321322
| 1337 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1337.java) | | Easy | String |
322323
| 1333 | [Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1333.java) | | Medium ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class _1414 {
6+
public static class Solution1 {
7+
/**
8+
* My completely original solution, heap is not really necessary, a regular array/list works out as well.
9+
*/
10+
public int findMinFibonacciNumbers(int k) {
11+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
12+
int one = 1;
13+
int two = 1;
14+
maxHeap.offer(one);
15+
maxHeap.offer(two);
16+
int three = one + two;
17+
while (three <= k) {
18+
maxHeap.offer(three);
19+
int tmp = two;
20+
two = three;
21+
one = tmp;
22+
three = one + two;
23+
}
24+
int minNumbers = 0;
25+
while (k > 0) {
26+
if (maxHeap.contains(k)) {
27+
minNumbers++;
28+
return minNumbers;
29+
} else {
30+
while (maxHeap.peek() > k) {
31+
maxHeap.poll();
32+
}
33+
Integer max = maxHeap.poll();
34+
k -= max;
35+
minNumbers++;
36+
}
37+
}
38+
return minNumbers;
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.secondthousand;
2+
3+
import com.fishercoder.solutions.secondthousand._1414;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _1414Test {
10+
private static _1414.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _1414.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.findMinFibonacciNumbers(7));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.findMinFibonacciNumbers(19));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)