Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# [1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K][title]

## 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 for n > 2.`

It is guaranteed that for the given constraints we can always find such Fibonacci numbers that sum up to `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.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(k int) int {
fb := []int{1, 1}
for i := 2; i < 44 && fb[i-1]+fb[i-2] <= k; i++ {
fb = append(fb, fb[i-1]+fb[i-2])
}
ret := 0
// 5
for index := len(fb) - 1; index >= 0 && k > 0; index-- {
if k < fb[index] {
continue
}
ret += k / fb[index]
k %= fb[index]
}
return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 7, 2},
{"TestCase2", 10, 2},
{"TestCase3", 19, 3},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading