Find the Minimum Number of Fibonacci Numbers Whose Sum Is K LeetCode solution in Java
1class Solution {2public int findMinFibonacciNumbers(int k) {3int ans = 0;45while (k > 0) {6// Run until solution is reached7int fib2prev = 1;8int fib1prev = 1;9while (fib1prev <= k) {10// Generate Fib values, stop when fib1prev is > k, we have the fib number we want stored in11// fib2prev12int temp = fib2prev + fib1prev;13fib2prev = fib1prev;14fib1prev = temp;15}16k -= fib2prev;17ans += 1;18}19return ans;20}21}