Description
Given an integerΒ k
, return the minimum number of Fibonacci numbers whose sum is equal to k
. The same Fibonacci number can be used multiple times.
The Fibonacci numbers are defined as:
F1 = 1
F2 = 1
Fn = Fn-1 + Fn-2
forn > 2.
k
.
Β
Example 1:
Input: k = 7 Output: 2 Explanation: The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, ... For k = 7 we can use 2 + 5 = 7.
Example 2:
Input: k = 10 Output: 2 Explanation: For k = 10 we can use 2 + 8 = 10.
Example 3:
Input: k = 19 Output: 3 Explanation: For k = 19 we can use 1 + 5 + 13 = 19.
Β
Constraints:
1 <= k <= 109
Solution
Python3
class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
fib = [1,1]
while fib[-1] < k:
fib.append(fib[-1] + fib[-2])
res = 0
i = len(fib) - 1
while k > 0:
res += k // fib[i]
k %= fib[i]
i -= 1
return res