Skip to content

Commit 8c5269d

Browse files
Add explanation and solution for LeetCode problem 1515 (Find the Minimum Number of Fibonacci Numbers Whose Sum Is K)
1 parent ad2075f commit 8c5269d

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

explanations/1515/en.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
## 1515. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K [Medium]
2+
3+
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k
4+
5+
## Description
6+
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.
7+
8+
The Fibonacci numbers are defined as:
9+
10+
- `F₁ = 1`
11+
- `F₂ = 1`
12+
- `Fₙ = Fₙ₋₁ + Fₙ₋₂` for `n > 2`
13+
14+
It is guaranteed that for the given constraints we can always find such Fibonacci numbers that sum up to `k`.
15+
16+
**Examples**
17+
18+
```tex
19+
Example 1:
20+
Input: k = 7
21+
Output: 2
22+
Explanation: The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, ...
23+
For k = 7 we can use 2 + 5 = 7.
24+
25+
Example 2:
26+
Input: k = 10
27+
Output: 2
28+
Explanation: For k = 10 we can use 2 + 8 = 10.
29+
30+
Example 3:
31+
Input: k = 19
32+
Output: 3
33+
Explanation: For k = 19 we can use 1 + 5 + 13 = 19.
34+
```
35+
36+
**Constraints**
37+
```tex
38+
- 1 <= k <= 10^9
39+
```
40+
41+
## Explanation
42+
43+
### Strategy
44+
Let's restate the problem: You're given a target integer `k`, and you need to find the minimum number of Fibonacci numbers that sum to `k`. You can use the same Fibonacci number multiple times, and it's guaranteed that a solution exists.
45+
46+
This is a **greedy problem** that can be solved by always choosing the largest possible Fibonacci number that doesn't exceed the remaining sum.
47+
48+
**What is given?** A target integer `k` that can be very large (up to 10⁹).
49+
50+
**What is being asked?** Find the minimum number of Fibonacci numbers that sum to `k`.
51+
52+
**Constraints:** The target `k` can be up to 10⁹, which means we need an efficient approach.
53+
54+
**Edge cases:**
55+
- `k = 1` (single Fibonacci number)
56+
- `k` is itself a Fibonacci number
57+
- `k` requires multiple Fibonacci numbers
58+
59+
**High-level approach:**
60+
The solution involves generating Fibonacci numbers up to `k`, then using a greedy approach to always select the largest possible Fibonacci number that fits in the remaining sum.
61+
62+
**Decomposition:**
63+
1. **Generate Fibonacci numbers**: Create all Fibonacci numbers up to `k`
64+
2. **Greedy selection**: Always choose the largest Fibonacci number that fits
65+
3. **Subtract and repeat**: Subtract the chosen number and continue until sum reaches 0
66+
4. **Count operations**: Track how many Fibonacci numbers were used
67+
68+
**Brute force vs. optimized strategy:**
69+
- **Brute force**: Try all combinations of Fibonacci numbers. This is extremely inefficient.
70+
- **Optimized**: Use greedy approach with pre-generated Fibonacci numbers. This takes O(log k) time.
71+
72+
### Steps
73+
Let's walk through the solution step by step using the first example: `k = 7`
74+
75+
**Step 1: Generate Fibonacci numbers up to k**
76+
- Start with F₁ = 1, F₂ = 1
77+
- F₃ = F₂ + F₁ = 1 + 1 = 2
78+
- F₄ = F₃ + F₂ = 2 + 1 = 3
79+
- F₅ = F₄ + F₃ = 3 + 2 = 5
80+
- F₆ = F₅ + F₄ = 5 + 3 = 8 (exceeds 7, stop)
81+
- Fibonacci numbers up to 7: [1, 1, 2, 3, 5]
82+
83+
**Step 2: Greedy selection process**
84+
- **Remaining sum**: 7
85+
- **Largest Fibonacci ≤ 7**: 5
86+
- **Use 5**: 7 - 5 = 2
87+
- **Count**: 1
88+
89+
- **Remaining sum**: 2
90+
- **Largest Fibonacci ≤ 2**: 2
91+
- **Use 2**: 2 - 2 = 0
92+
- **Count**: 2
93+
94+
- **Remaining sum**: 0
95+
- **Process complete**
96+
97+
**Step 3: Result**
98+
- Total Fibonacci numbers used: 2
99+
- Solution: 5 + 2 = 7
100+
101+
**Why this works:**
102+
The greedy approach works because:
103+
1. **Optimal substructure**: If we can represent `k` with `n` Fibonacci numbers, then representing `k - F_max` with `n-1` numbers must also be optimal
104+
2. **Greedy choice property**: Always choosing the largest possible Fibonacci number ensures we use the minimum number of terms
105+
3. **Fibonacci properties**: Each Fibonacci number is the sum of the two preceding ones, making the greedy choice optimal
106+
107+
> **Note:** The key insight is that using the largest possible Fibonacci number at each step minimizes the total count. This works because Fibonacci numbers have the property that no smaller combination can sum to a larger value more efficiently.
108+
109+
**Time Complexity:** O(log k) - we generate Fibonacci numbers up to k, which grows exponentially
110+
**Space Complexity:** O(log k) - we store the generated Fibonacci numbers

solutions/1515/01.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def findMinFibonacciNumbers(k):
2+
"""
3+
Find the minimum number of Fibonacci numbers whose sum equals k.
4+
5+
Args:
6+
k: int - Target sum to achieve
7+
8+
Returns:
9+
int - Minimum number of Fibonacci numbers needed
10+
"""
11+
# Generate Fibonacci numbers up to k
12+
fib_numbers = [1, 1]
13+
14+
# Keep generating Fibonacci numbers until we exceed k
15+
while fib_numbers[-1] <= k:
16+
next_fib = fib_numbers[-1] + fib_numbers[-2]
17+
if next_fib > k:
18+
break
19+
fib_numbers.append(next_fib)
20+
21+
# Use greedy approach: always choose largest possible Fibonacci number
22+
count = 0
23+
remaining = k
24+
25+
# Start from the largest Fibonacci number and work backwards
26+
for i in range(len(fib_numbers) - 1, -1, -1):
27+
if fib_numbers[i] <= remaining:
28+
remaining -= fib_numbers[i]
29+
count += 1
30+
31+
# If we've reached the target, we're done
32+
if remaining == 0:
33+
break
34+
35+
return count

0 commit comments

Comments
 (0)