Problem Description
Given an integer k, find the minimum number of Fibonacci numbers whose sum equals k. The Fibonacci numbers are defined as F₁ = 1, F₂ = 1, and for n > 2, Fₙ = Fₙ₋₁ + Fₙ₋₂. It is allowed to use the same Fibonacci number multiple times. It is guaranteed that such a representation exists.
Key Insights
- Fibonacci numbers grow exponentially, so there are only O(log k) numbers less than or equal to k.
- A greedy strategy works: always subtract the largest Fibonacci number from k until k becomes 0.
- The problem guarantees a solution exists, ensuring the greedy approach is optimal for this scenario.
Space and Time Complexity
Time Complexity: O(log k)
Space Complexity: O(log k)
Solution
- Generate all Fibonacci numbers less than or equal to k.
- Iterate over the generated Fibonacci numbers in reverse order (from largest to smallest).
- For each Fibonacci number, determine how many times it can be subtracted from k and add that count to your answer.
- Subtract the appropriate amount from k and continue until k becomes 0.
- The sum of counts is the minimum number of Fibonacci numbers required.
This greedy approach works because using the largest possible Fibonacci numbers minimizes the number of terms needed to sum up to k.