From 196ee0e26737864e2c10817f4e71b3f44758154b Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 17 Sep 2025 09:07:16 +0800 Subject: [PATCH] Add solution and test-cases for problem 1414 --- .../README.md | 44 +++++++++++++++++++ .../Solution.go | 17 ++++++- .../Solution_test.go | 14 +++--- 3 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/README.md diff --git a/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/README.md b/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/README.md new file mode 100644 index 000000000..4a1104914 --- /dev/null +++ b/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/README.md @@ -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 diff --git a/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution.go b/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution.go index d115ccf5e..cba7d61ed 100755 --- a/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution.go +++ b/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution.go @@ -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 } diff --git a/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution_test.go b/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution_test.go index 14ff50eb4..840e0806b 100755 --- a/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution_test.go +++ b/leetcode/1401-1500/1414.Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }