|
| 1 | +// https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/ |
| 2 | + |
| 3 | +class Solution { |
| 4 | + |
| 5 | + public ArrayList<Integer> generateFib(int k) { |
| 6 | + |
| 7 | + // OBJECTIVE: Return a list of fibonacci numbers from 1 to k |
| 8 | + |
| 9 | + // Create a list |
| 10 | + ArrayList<Integer> fib = new ArrayList<>(); |
| 11 | + fib.add(1); |
| 12 | + fib.add(1); |
| 13 | + |
| 14 | + // Populate list |
| 15 | + int num = 1; |
| 16 | + while (num < k) { |
| 17 | + |
| 18 | + // Get the last 2 numbers |
| 19 | + int numA = fib.get(fib.size() - 1); |
| 20 | + int numB = fib.get(fib.size() - 2); |
| 21 | + |
| 22 | + // If new number is less than or equal to k, update num and add it to list |
| 23 | + if (numA + numB <= k) { |
| 24 | + num = numA + numB; |
| 25 | + fib.add(num); |
| 26 | + } |
| 27 | + |
| 28 | + // If new number is greater than k, exit loop |
| 29 | + else { |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return fib; |
| 35 | + } |
| 36 | + |
| 37 | + public int findK(ArrayList<Integer> fib, int n, int target) { |
| 38 | + |
| 39 | + // OBJECTIVE: Return index of target or number closest to it |
| 40 | + |
| 41 | + // If target equals to 0, exit function |
| 42 | + if (target == 0) {return 0;} |
| 43 | + |
| 44 | + // If target is greater than or equal to fib's last element, return it |
| 45 | + if (target >= fib.get(n - 1)) {return n - 1;} |
| 46 | + |
| 47 | + // Create left and right pointers |
| 48 | + int leftPtr = 0; |
| 49 | + int rightPtr = n - 1; |
| 50 | + |
| 51 | + // Do binary search to find target in fib |
| 52 | + while (leftPtr < rightPtr) { |
| 53 | + |
| 54 | + // Create a middle pointer |
| 55 | + int midPtr = (int) (leftPtr + rightPtr) / 2; |
| 56 | + |
| 57 | + // If middle element is greater than target, move rightPtr |
| 58 | + if (fib.get(midPtr) > target) { |
| 59 | + rightPtr = midPtr; |
| 60 | + } |
| 61 | + |
| 62 | + // If middle element is less than or equal to target, move leftPtr |
| 63 | + else { |
| 64 | + leftPtr = midPtr + 1; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Return left pointer |
| 69 | + return leftPtr - 1; |
| 70 | + } |
| 71 | + |
| 72 | + public int findMinFibonacciNumbers(int k) { |
| 73 | + |
| 74 | + // OBJECTIVE: Return the minimum number of fibonacci numbers whose sum is equal to k |
| 75 | + |
| 76 | + // If k equals to 1, return it |
| 77 | + if (k == 1) {return 1;} |
| 78 | + |
| 79 | + // Generate a list of fibonacci numbers from 1 to k |
| 80 | + ArrayList<Integer> fib = generateFib(k); |
| 81 | + int n = fib.size(); |
| 82 | + |
| 83 | + // If the last element equals to k, return 1 |
| 84 | + if (fib.get(n - 1) == k) {return 1;} |
| 85 | + |
| 86 | + // Create a variable to hold minimum number of fibonacci numbers |
| 87 | + int minLen = 0; |
| 88 | + |
| 89 | + // Get minimum number of fibonacci numbers |
| 90 | + while (k > 0) { |
| 91 | + |
| 92 | + // Find k (or value closest to k) in fib |
| 93 | + int idx = findK(fib, n, k); |
| 94 | + |
| 95 | + // Increment minLen |
| 96 | + minLen++; |
| 97 | + |
| 98 | + // Update k by subtracting idx element from it |
| 99 | + k -= fib.get(idx); |
| 100 | + } |
| 101 | + |
| 102 | + return minLen; |
| 103 | + } |
| 104 | +} |
0 commit comments