|
51 | 51 |
|
52 | 52 | <p><meta charset="UTF-8" />注意:本题与主站 873 题相同: <a href="https://leetcode-cn.com/problems/length-of-longest-fibonacci-subsequence/">https://leetcode-cn.com/problems/length-of-longest-fibonacci-subsequence/</a></p>
|
53 | 53 |
|
54 |
| - |
55 | 54 | ## 解法
|
56 | 55 |
|
57 | 56 | <!-- 这里可写通用的实现逻辑 -->
|
58 | 57 |
|
| 58 | +动态规划。 |
| 59 | + |
| 60 | +- 状态表示:`dp[j][i]` 表示斐波那契式最后两项为 `arr[j]`, `arr[i]` 时的最大子序列长度。 |
| 61 | +- 状态计算:`dp[j][i] = dp[k][j] + 1`(当且仅当 `k < j < i`,并且 `arr[k] + arr[j] == arr[i]`), `ans = max(ans, dp[j][i])`。 |
| 62 | + |
59 | 63 | <!-- tabs:start -->
|
60 | 64 |
|
61 | 65 | ### **Python3**
|
62 | 66 |
|
63 | 67 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
64 | 68 |
|
65 | 69 | ```python
|
66 |
| - |
| 70 | +class Solution: |
| 71 | + def lenLongestFibSubseq(self, arr: List[int]) -> int: |
| 72 | + mp = {v: i for i, v in enumerate(arr)} |
| 73 | + n = len(arr) |
| 74 | + dp = [[0] * n for _ in range(n)] |
| 75 | + for i in range(n): |
| 76 | + for j in range(i): |
| 77 | + dp[j][i] = 2 |
| 78 | + ans = 0 |
| 79 | + for i in range(n): |
| 80 | + for j in range(i): |
| 81 | + delta = arr[i] - arr[j] |
| 82 | + if delta in mp: |
| 83 | + k = mp[delta] |
| 84 | + if k < j: |
| 85 | + dp[j][i] = dp[k][j] + 1 |
| 86 | + ans = max(ans, dp[j][i]) |
| 87 | + return ans |
67 | 88 | ```
|
68 | 89 |
|
69 | 90 | ### **Java**
|
70 | 91 |
|
71 | 92 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
72 | 93 |
|
73 | 94 | ```java
|
| 95 | +class Solution { |
| 96 | + public int lenLongestFibSubseq(int[] arr) { |
| 97 | + int n = arr.length; |
| 98 | + Map<Integer, Integer> mp = new HashMap<>(); |
| 99 | + for (int i = 0; i < n; ++i) { |
| 100 | + mp.put(arr[i], i); |
| 101 | + } |
| 102 | + int[][] dp = new int[n][n]; |
| 103 | + for (int i = 0; i < n; ++i) { |
| 104 | + for (int j = 0; j < i; ++j) { |
| 105 | + dp[j][i] = 2; |
| 106 | + } |
| 107 | + } |
| 108 | + int ans = 0; |
| 109 | + for (int i = 0; i < n; ++i) { |
| 110 | + for (int j = 0; j < i; ++j) { |
| 111 | + int delta = arr[i] - arr[j]; |
| 112 | + if (mp.containsKey(delta)) { |
| 113 | + int k = mp.get(delta); |
| 114 | + if (k < j) { |
| 115 | + dp[j][i] = dp[k][j] + 1; |
| 116 | + ans = Math.max(ans, dp[j][i]); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return ans; |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### **C++** |
| 127 | + |
| 128 | +```cpp |
| 129 | +class Solution { |
| 130 | +public: |
| 131 | + int lenLongestFibSubseq(vector<int>& arr) { |
| 132 | + unordered_map<int, int> mp; |
| 133 | + int n = arr.size(); |
| 134 | + for (int i = 0; i < n; ++i) mp[arr[i]] = i; |
| 135 | + vector<vector<int>> dp(n, vector<int>(n)); |
| 136 | + for (int i = 0; i < n; ++i) |
| 137 | + { |
| 138 | + for (int j = 0; j < i; ++j) |
| 139 | + dp[j][i] = 2; |
| 140 | + } |
| 141 | + int ans = 0; |
| 142 | + for (int i = 0; i < n; ++i) |
| 143 | + { |
| 144 | + for (int j = 0; j < i; ++j) |
| 145 | + { |
| 146 | + int delta = arr[i] - arr[j]; |
| 147 | + if (mp.count(delta)) |
| 148 | + { |
| 149 | + int k = mp[delta]; |
| 150 | + if (k < j) |
| 151 | + { |
| 152 | + dp[j][i] = dp[k][j] + 1; |
| 153 | + ans = max(ans, dp[j][i]); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + return ans; |
| 159 | + } |
| 160 | +}; |
| 161 | +``` |
74 | 162 |
|
| 163 | +### **Go** |
| 164 | +
|
| 165 | +```go |
| 166 | +func lenLongestFibSubseq(arr []int) int { |
| 167 | + n := len(arr) |
| 168 | + mp := make(map[int]int, n) |
| 169 | + for i, v := range arr { |
| 170 | + mp[v] = i + 1 |
| 171 | + } |
| 172 | + dp := make([][]int, n) |
| 173 | + for i := 0; i < n; i++ { |
| 174 | + dp[i] = make([]int, n) |
| 175 | + for j := 0; j < i; j++ { |
| 176 | + dp[j][i] = 2 |
| 177 | + } |
| 178 | + } |
| 179 | + ans := 0 |
| 180 | + for i := 0; i < n; i++ { |
| 181 | + for j := 0; j < i; j++ { |
| 182 | + delta := arr[i] - arr[j] |
| 183 | + k := mp[delta] - 1 |
| 184 | + if k >= 0 && k < j { |
| 185 | + dp[j][i] = dp[k][j] + 1 |
| 186 | + ans = max(ans, dp[j][i]) |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + return ans |
| 191 | +} |
| 192 | +
|
| 193 | +func max(a, b int) int { |
| 194 | + if a > b { |
| 195 | + return a |
| 196 | + } |
| 197 | + return b |
| 198 | +} |
75 | 199 | ```
|
76 | 200 |
|
77 | 201 | ### **...**
|
|
0 commit comments