diff --git a/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/README.md b/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/README.md index a3ebd0fd4..d6a9a2abe 100644 --- a/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/README.md +++ b/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/README.md @@ -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]. +``` ## 结语 diff --git a/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution.go b/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution.go index d115ccf5e..9c7e92004 100644 --- a/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution.go +++ b/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution.go @@ -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 } diff --git a/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution_test.go b/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution_test.go index 14ff50eb4..7bb24cd06 100644 --- a/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution_test.go +++ b/leetcode/801-900/0873.Length-of-Longest-Fibonacci-Subsequence/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }