Skip to content

Commit 06dad5e

Browse files
authored
Merge pull request #1316 from 0xff-dev/1414
Add solution and test-cases for problem 1414
2 parents f03226b + 196ee0e commit 06dad5e

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K][title]
2+
3+
## Description
4+
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.
5+
6+
The Fibonacci numbers are defined as:
7+
8+
- `F1 = 1`
9+
- `F2 = 1`
10+
- `Fn = Fn-1 + Fn-2 for n > 2.`
11+
12+
It is guaranteed that for the given constraints we can always find such Fibonacci numbers that sum up to `k`.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: k = 7
18+
Output: 2
19+
Explanation: The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, ...
20+
For k = 7 we can use 2 + 5 = 7.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: k = 10
27+
Output: 2
28+
Explanation: For k = 10 we can use 2 + 8 = 10.
29+
```
30+
31+
**Example 3:**
32+
33+
```
34+
Input: k = 19
35+
Output: 3
36+
Explanation: For k = 19 we can use 1 + 5 + 13 = 19.
37+
```
38+
39+
## 结语
40+
41+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
42+
43+
[title]: https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k
44+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(k int) int {
4+
fb := []int{1, 1}
5+
for i := 2; i < 44 && fb[i-1]+fb[i-2] <= k; i++ {
6+
fb = append(fb, fb[i-1]+fb[i-2])
7+
}
8+
ret := 0
9+
// 5
10+
for index := len(fb) - 1; index >= 0 && k > 0; index-- {
11+
if k < fb[index] {
12+
continue
13+
}
14+
ret += k / fb[index]
15+
k %= fb[index]
16+
}
17+
return ret
518
}

leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 7, 2},
17+
{"TestCase2", 10, 2},
18+
{"TestCase3", 19, 3},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
//压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)