Skip to content

Commit

Permalink
Merge pull request #907 from 0xff-dev/873
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 873
  • Loading branch information
6boris committed Jun 16, 2024
2 parents 6d344f0 + cdf8796 commit 1940a25
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# [873.Length of Longest Fibonacci Subsequence][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
A sequence `x1, x2, ..., xn` is Fibonacci-like if:

- `n >= 3`
- `xi + xi+1 == xi+2` for all `i + 2 <= n`

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`.

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]`.

**Example 1:**

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

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Length of Longest Fibonacci Subsequence
```go
```

Input: arr = [1,3,7,11,12,14,18]
Output: 3
Explanation: The longest subsequence that is fibonacci-like: [1,11,12], [3,11,14] or [7,11,18].
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(arr []int) int {
nums := make(map[int]struct{})
for _, n := range arr {
nums[n] = struct{}{}
}
var try func(int, int, *int)
try = func(a, b int, c *int) {
_, ok := nums[a+b]
if !ok {
return
}
*c++
try(b, a+b, c)
}
ans := 0
for i := 0; i < len(arr)-1; i++ {
for j := i + 1; j < len(arr); j++ {
c := 0
try(arr[i], arr[j], &c)
if c != 0 {
ans = max(ans, c+2)
}
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ 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", []int{1, 2, 3, 4, 5, 6, 7, 8}, 5},
{"TestCase2", []int{1, 3, 7, 11, 12, 14, 18}, 3},
}

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

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

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

0 comments on commit 1940a25

Please sign in to comment.