Find the Minimum Number of Fibonacci Numbers Whose Sum Is K - Problem
Given an integer k, find the minimum number of Fibonacci numbers whose sum equals k. The same Fibonacci number can be used multiple times.
The Fibonacci sequence is defined as:
F₁ = 1F₂ = 1Fₙ = Fₙ₋₁ + Fₙ₋₂forn > 2
So the sequence starts: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
Goal: Return the minimum count of Fibonacci numbers that sum to k.
Note: It's guaranteed that a solution always exists for the given constraints.
Input & Output
example_1.py — Basic case
$
Input:
k = 7
›
Output:
2
💡 Note:
The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, ... We can represent 7 as 5 + 2, which uses 2 Fibonacci numbers. This is optimal since we can't represent 7 with just 1 Fibonacci number.
example_2.py — Perfect Fibonacci
$
Input:
k = 10
›
Output:
2
💡 Note:
We can represent 10 as 8 + 2. Both 8 and 2 are Fibonacci numbers. We use the greedy approach: pick the largest possible (8), then the largest possible from what remains (2).
example_3.py — Small value
$
Input:
k = 4
›
Output:
2
💡 Note:
We can represent 4 as 3 + 1. Although we could also use 2 + 1 + 1, the greedy approach gives us 3 + 1 which uses only 2 numbers and is optimal.
Constraints
- 1 ≤ k ≤ 109
- Note: k can be very large, but there are only ~45 Fibonacci numbers ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Generate Fibonacci Sequence
Create all Fibonacci numbers up to k: [1, 1, 2, 3, 5, 8, 13, 21, 34, ...]
2
Pick Largest Possible
Always choose the biggest Fibonacci number that doesn't exceed remaining k
3
Subtract and Repeat
Reduce k by chosen number and increment counter, repeat until k = 0
4
Return Count
The counter holds the minimum number of Fibonacci numbers needed
Key Takeaway
🎯 Key Insight: Fibonacci numbers have the unique property that F(n+1) > F(n) + F(n-1) + ... + F(1), making the greedy approach optimal!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code