From 7c1dfdfa16636794086a9dde871de342677c28f2 Mon Sep 17 00:00:00 2001
From: acbin <44314231+acbin@users.noreply.github.com>
Date: Thu, 20 Mar 2025 12:33:17 +0000
Subject: [PATCH 1/2] style: format code and docs with prettier
---
solution/0800-0899/0830.Positions of Large Groups/README.md | 2 --
1 file changed, 2 deletions(-)
diff --git a/solution/0800-0899/0830.Positions of Large Groups/README.md b/solution/0800-0899/0830.Positions of Large Groups/README.md
index 9a29de77e5560..389aa0187ce17 100644
--- a/solution/0800-0899/0830.Positions of Large Groups/README.md
+++ b/solution/0800-0899/0830.Positions of Large Groups/README.md
@@ -58,8 +58,6 @@ tags:
输出:[]
-
-
提示:
From 96eee952890e11b88c4bd68b44aacc13e9debaa9 Mon Sep 17 00:00:00 2001
From: acbin <1320909796@qq.com>
Date: Fri, 21 Mar 2025 01:39:29 +0000
Subject: [PATCH 2/2] feat: add solutions to lc problem: No.1414
No.1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K
---
.../README.md | 152 +++++++++++-------
.../README_EN.md | 152 +++++++++++-------
.../Solution.cpp | 18 ++-
.../Solution.go | 21 ++-
.../Solution.java | 21 ++-
.../Solution.py | 19 +--
.../Solution.rs | 33 ++--
.../Solution.ts | 32 ++--
8 files changed, 272 insertions(+), 176 deletions(-)
diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/README.md b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/README.md
index e315f883107e5..17981f4574745 100644
--- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/README.md
+++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/README.md
@@ -68,7 +68,13 @@ tags:
-### 方法一
+### 方法一:贪心
+
+我们可以每次贪心地选取一个不超过 $k$ 的最大的斐波那契数,然后将 $k$ 减去该数,答案加一,一直循环,直到 $k = 0$ 为止。
+
+由于每次贪心地选取了最大的不超过 $k$ 的斐波那契数,假设为 $b$,前一个数为 $a$,后一个数为 $c$。将 $k$ 减去 $b$,得到的结果,一定小于 $a$,也即意味着,我们选取了 $b$ 之后,一定不会选到 $a$。这是因为,如果能选上 $a$,那么我们在前面就可以贪心地选上 $b$ 的下一个斐波那契数 $c$,这不符合我们的假设。因此,我们在选取 $b$ 之后,可以贪心地减小斐波那契数。
+
+时间复杂度 $O(\log k)$,空间复杂度 $O(1)$。
@@ -77,32 +83,40 @@ tags:
```python
class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
- def dfs(k):
- if k < 2:
- return k
- a = b = 1
- while b <= k:
- a, b = b, a + b
- return 1 + dfs(k - a)
-
- return dfs(k)
+ a = b = 1
+ while b <= k:
+ a, b = b, a + b
+ ans = 0
+ while k:
+ if k >= b:
+ k -= b
+ ans += 1
+ a, b = b - a, a
+ return ans
```
#### Java
```java
class Solution {
-
public int findMinFibonacciNumbers(int k) {
- if (k < 2) {
- return k;
- }
int a = 1, b = 1;
while (b <= k) {
- b = a + b;
- a = b - a;
+ int c = a + b;
+ a = b;
+ b = c;
}
- return 1 + findMinFibonacciNumbers(k - a);
+ int ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ++ans;
+ }
+ int c = b - a;
+ b = a;
+ a = c;
+ }
+ return ans;
}
}
```
@@ -113,13 +127,23 @@ class Solution {
class Solution {
public:
int findMinFibonacciNumbers(int k) {
- if (k < 2) return k;
int a = 1, b = 1;
while (b <= k) {
- b = a + b;
- a = b - a;
+ int c = a + b;
+ a = b;
+ b = c;
}
- return 1 + findMinFibonacciNumbers(k - a);
+ int ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ++ans;
+ }
+ int c = b - a;
+ b = a;
+ a = c;
+ }
+ return ans;
}
};
```
@@ -127,66 +151,76 @@ public:
#### Go
```go
-func findMinFibonacciNumbers(k int) int {
- if k < 2 {
- return k
- }
+func findMinFibonacciNumbers(k int) (ans int) {
a, b := 1, 1
for b <= k {
- a, b = b, a+b
+ c := a + b
+ a = b
+ b = c
}
- return 1 + findMinFibonacciNumbers(k-a)
+
+ for k > 0 {
+ if k >= b {
+ k -= b
+ ans++
+ }
+ c := b - a
+ b = a
+ a = c
+ }
+ return
}
```
#### TypeScript
```ts
-const arr = [
- 1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986,
- 39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229,
- 317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610,
- 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1,
-];
-
function findMinFibonacciNumbers(k: number): number {
- let res = 0;
- for (const num of arr) {
- if (k >= num) {
- k -= num;
- res++;
- if (k === 0) {
- break;
- }
+ let [a, b] = [1, 1];
+ while (b <= k) {
+ let c = a + b;
+ a = b;
+ b = c;
+ }
+
+ let ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ans++;
}
+ let c = b - a;
+ b = a;
+ a = c;
}
- return res;
+ return ans;
}
```
#### Rust
```rust
-const FIB: [i32; 45] = [
- 1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986,
- 39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229,
- 317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610,
- 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1,
-];
-
impl Solution {
pub fn find_min_fibonacci_numbers(mut k: i32) -> i32 {
- let mut res = 0;
- for &i in FIB.into_iter() {
- if k >= i {
- k -= i;
- res += 1;
- if k == 0 {
- break;
- }
+ let mut a = 1;
+ let mut b = 1;
+ while b <= k {
+ let c = a + b;
+ a = b;
+ b = c;
+ }
+
+ let mut ans = 0;
+ while k > 0 {
+ if k >= b {
+ k -= b;
+ ans += 1;
}
+ let c = b - a;
+ b = a;
+ a = c;
}
- res
+ ans
}
}
```
diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/README_EN.md b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/README_EN.md
index cb834d3147016..1ce6c8755d345 100644
--- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/README_EN.md
+++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/README_EN.md
@@ -67,7 +67,13 @@ For k = 7 we can use 2 + 5 = 7.
-### Solution 1
+### Solution 1: Greedy
+
+We can greedily select the largest Fibonacci number that does not exceed $k$ each time, then subtract this number from $k$ and increment the answer by one. This process is repeated until $k = 0$.
+
+Since we greedily select the largest Fibonacci number that does not exceed $k$ each time, suppose this number is $b$, the previous number is $a$, and the next number is $c$. Subtracting $b$ from $k$ results in a value that is less than $a$, which means that after selecting $b$, we will not select $a$. This is because if we could select $a$, then we could have greedily selected the next Fibonacci number $c$ instead of $b$ earlier, which contradicts our assumption. Therefore, after selecting $b$, we can greedily reduce the Fibonacci number.
+
+The time complexity is $O(\log k)$, and the space complexity is $O(1)$.
@@ -76,32 +82,40 @@ For k = 7 we can use 2 + 5 = 7.
```python
class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
- def dfs(k):
- if k < 2:
- return k
- a = b = 1
- while b <= k:
- a, b = b, a + b
- return 1 + dfs(k - a)
-
- return dfs(k)
+ a = b = 1
+ while b <= k:
+ a, b = b, a + b
+ ans = 0
+ while k:
+ if k >= b:
+ k -= b
+ ans += 1
+ a, b = b - a, a
+ return ans
```
#### Java
```java
class Solution {
-
public int findMinFibonacciNumbers(int k) {
- if (k < 2) {
- return k;
- }
int a = 1, b = 1;
while (b <= k) {
- b = a + b;
- a = b - a;
+ int c = a + b;
+ a = b;
+ b = c;
}
- return 1 + findMinFibonacciNumbers(k - a);
+ int ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ++ans;
+ }
+ int c = b - a;
+ b = a;
+ a = c;
+ }
+ return ans;
}
}
```
@@ -112,13 +126,23 @@ class Solution {
class Solution {
public:
int findMinFibonacciNumbers(int k) {
- if (k < 2) return k;
int a = 1, b = 1;
while (b <= k) {
- b = a + b;
- a = b - a;
+ int c = a + b;
+ a = b;
+ b = c;
}
- return 1 + findMinFibonacciNumbers(k - a);
+ int ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ++ans;
+ }
+ int c = b - a;
+ b = a;
+ a = c;
+ }
+ return ans;
}
};
```
@@ -126,66 +150,76 @@ public:
#### Go
```go
-func findMinFibonacciNumbers(k int) int {
- if k < 2 {
- return k
- }
+func findMinFibonacciNumbers(k int) (ans int) {
a, b := 1, 1
for b <= k {
- a, b = b, a+b
+ c := a + b
+ a = b
+ b = c
}
- return 1 + findMinFibonacciNumbers(k-a)
+
+ for k > 0 {
+ if k >= b {
+ k -= b
+ ans++
+ }
+ c := b - a
+ b = a
+ a = c
+ }
+ return
}
```
#### TypeScript
```ts
-const arr = [
- 1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986,
- 39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229,
- 317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610,
- 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1,
-];
-
function findMinFibonacciNumbers(k: number): number {
- let res = 0;
- for (const num of arr) {
- if (k >= num) {
- k -= num;
- res++;
- if (k === 0) {
- break;
- }
+ let [a, b] = [1, 1];
+ while (b <= k) {
+ let c = a + b;
+ a = b;
+ b = c;
+ }
+
+ let ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ans++;
}
+ let c = b - a;
+ b = a;
+ a = c;
}
- return res;
+ return ans;
}
```
#### Rust
```rust
-const FIB: [i32; 45] = [
- 1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986,
- 39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229,
- 317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610,
- 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1,
-];
-
impl Solution {
pub fn find_min_fibonacci_numbers(mut k: i32) -> i32 {
- let mut res = 0;
- for &i in FIB.into_iter() {
- if k >= i {
- k -= i;
- res += 1;
- if k == 0 {
- break;
- }
+ let mut a = 1;
+ let mut b = 1;
+ while b <= k {
+ let c = a + b;
+ a = b;
+ b = c;
+ }
+
+ let mut ans = 0;
+ while k > 0 {
+ if k >= b {
+ k -= b;
+ ans += 1;
}
+ let c = b - a;
+ b = a;
+ a = c;
}
- res
+ ans
}
}
```
diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.cpp b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.cpp
index ad9f584e60e8b..82c02f2d273b2 100644
--- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.cpp
+++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.cpp
@@ -1,12 +1,22 @@
class Solution {
public:
int findMinFibonacciNumbers(int k) {
- if (k < 2) return k;
int a = 1, b = 1;
while (b <= k) {
- b = a + b;
- a = b - a;
+ int c = a + b;
+ a = b;
+ b = c;
}
- return 1 + findMinFibonacciNumbers(k - a);
+ int ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ++ans;
+ }
+ int c = b - a;
+ b = a;
+ a = c;
+ }
+ return ans;
}
};
\ No newline at end of file
diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.go b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.go
index d843502303ee1..3290b4d6bd12f 100644
--- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.go
+++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.go
@@ -1,10 +1,19 @@
-func findMinFibonacciNumbers(k int) int {
- if k < 2 {
- return k
- }
+func findMinFibonacciNumbers(k int) (ans int) {
a, b := 1, 1
for b <= k {
- a, b = b, a+b
+ c := a + b
+ a = b
+ b = c
+ }
+
+ for k > 0 {
+ if k >= b {
+ k -= b
+ ans++
+ }
+ c := b - a
+ b = a
+ a = c
}
- return 1 + findMinFibonacciNumbers(k-a)
+ return
}
\ No newline at end of file
diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.java b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.java
index dfc3b8ae6d741..3d26e1803fdd0 100644
--- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.java
+++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.java
@@ -1,14 +1,21 @@
class Solution {
-
public int findMinFibonacciNumbers(int k) {
- if (k < 2) {
- return k;
- }
int a = 1, b = 1;
while (b <= k) {
- b = a + b;
- a = b - a;
+ int c = a + b;
+ a = b;
+ b = c;
+ }
+ int ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ++ans;
+ }
+ int c = b - a;
+ b = a;
+ a = c;
}
- return 1 + findMinFibonacciNumbers(k - a);
+ return ans;
}
}
\ No newline at end of file
diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.py b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.py
index 02e1caea2900d..8575a1fb9e5cd 100644
--- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.py
+++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.py
@@ -1,11 +1,12 @@
class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
- def dfs(k):
- if k < 2:
- return k
- a = b = 1
- while b <= k:
- a, b = b, a + b
- return 1 + dfs(k - a)
-
- return dfs(k)
+ a = b = 1
+ while b <= k:
+ a, b = b, a + b
+ ans = 0
+ while k:
+ if k >= b:
+ k -= b
+ ans += 1
+ a, b = b - a, a
+ return ans
diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.rs b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.rs
index cbcb2a4b1b436..a22687f27f4d0 100644
--- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.rs
+++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.rs
@@ -1,22 +1,23 @@
-const FIB: [i32; 45] = [
- 1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986,
- 39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229,
- 317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610,
- 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1,
-];
-
impl Solution {
pub fn find_min_fibonacci_numbers(mut k: i32) -> i32 {
- let mut res = 0;
- for &i in FIB.into_iter() {
- if k >= i {
- k -= i;
- res += 1;
- if k == 0 {
- break;
- }
+ let mut a = 1;
+ let mut b = 1;
+ while b <= k {
+ let c = a + b;
+ a = b;
+ b = c;
+ }
+
+ let mut ans = 0;
+ while k > 0 {
+ if k >= b {
+ k -= b;
+ ans += 1;
}
+ let c = b - a;
+ b = a;
+ a = c;
}
- res
+ ans
}
}
diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.ts b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.ts
index 9a819f5219523..79ecb26332b63 100644
--- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.ts
+++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.ts
@@ -1,20 +1,20 @@
-const arr = [
- 1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986,
- 39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229,
- 317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610,
- 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1,
-];
-
function findMinFibonacciNumbers(k: number): number {
- let res = 0;
- for (const num of arr) {
- if (k >= num) {
- k -= num;
- res++;
- if (k === 0) {
- break;
- }
+ let [a, b] = [1, 1];
+ while (b <= k) {
+ let c = a + b;
+ a = b;
+ b = c;
+ }
+
+ let ans = 0;
+ while (k > 0) {
+ if (k >= b) {
+ k -= b;
+ ans++;
}
+ let c = b - a;
+ b = a;
+ a = c;
}
- return res;
+ return ans;
}