Skip to content

Commit 1940a25

Browse files
authored
Merge pull request #907 from 0xff-dev/873
Add solution and test-cases for problem 873
2 parents 6d344f0 + cdf8796 commit 1940a25

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [873.Length of Longest Fibonacci Subsequence][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
A sequence `x1, x2, ..., xn` is Fibonacci-like if:
5+
6+
- `n >= 3`
7+
- `xi + xi+1 == xi+2` for all `i + 2 <= n`
8+
9+
Given a **strictly increasing** array `arr` of positive integers forming a sequence, return the **length** of the longest Fibonacci-like subsequence of `arr`. If one does not exist, return `0`.
10+
11+
A **subsequence** is derived from another sequence `arr` by deleting any number of elements (including none) from `arr`, without changing the order of the remaining elements. For example, `[3, 5, 8]` is a subsequence of `[3, 4, 5, 6, 7, 8]`.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: arr = [1,2,3,4,5,6,7,8]
17+
Output: 5
18+
Explanation: The longest subsequence that is fibonacci-like: [1,2,3,5,8].
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Length of Longest Fibonacci Subsequence
23-
```go
2423
```
25-
24+
Input: arr = [1,3,7,11,12,14,18]
25+
Output: 3
26+
Explanation: The longest subsequence that is fibonacci-like: [1,11,12], [3,11,14] or [7,11,18].
27+
```
2628

2729
## 结语
2830

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(arr []int) int {
4+
nums := make(map[int]struct{})
5+
for _, n := range arr {
6+
nums[n] = struct{}{}
7+
}
8+
var try func(int, int, *int)
9+
try = func(a, b int, c *int) {
10+
_, ok := nums[a+b]
11+
if !ok {
12+
return
13+
}
14+
*c++
15+
try(b, a+b, c)
16+
}
17+
ans := 0
18+
for i := 0; i < len(arr)-1; i++ {
19+
for j := i + 1; j < len(arr); j++ {
20+
c := 0
21+
try(arr[i], arr[j], &c)
22+
if c != 0 {
23+
ans = max(ans, c+2)
24+
}
25+
}
26+
}
27+
return ans
528
}

leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ 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", []int{1, 2, 3, 4, 5, 6, 7, 8}, 5},
17+
{"TestCase2", []int{1, 3, 7, 11, 12, 14, 18}, 3},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)