873. Length of Longest Fibonacci Subsequence
of length Longest Subsequence Fibonacci
2023-09-11 14:22:44 时间
A sequence
X_1, X_2, ..., X_n
is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2}
for alli + 2 <= n
Given a strictly increasing array
A
of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence ofA
. If one does not exist, return 0.(Recall that a subsequence is derived from another sequence
A
by deleting any number of elements (including none) fromA
, 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: [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:
Input: [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].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
- (The time limit has been reduced by 50% for submissions in Java, C, and C++.)
Approach #1: unordered_map. [C++]
class Solution { public: int lenLongestFibSubseq(vector<int>& A) { unordered_map<int, int> memo; int len = A.size(); int ans = 0, temp = 0; for (int i = 0; i < len; ++i) memo[A[i]] = i; for (int i = 0; i < len; ++i) { for (int j = i + 1; j < len; ++j) { int ant = 2; int last_idx = i; for (int cur_idx = j; cur_idx < len; ) { temp = A[last_idx] + A[cur_idx]; if (memo.count(temp)) { ant++; last_idx = cur_idx; cur_idx = memo[temp]; } else break; } ans = max(ans, ant); } } return ans == 2 ? 0 : ans; } };
Approach #2: DP. [Java]
class Solution { public int lenLongestFibSubseq(int[] A) { int n = A.length; int res = 0; int[][] dp = new int[n+1][n+1]; for (int[] row : dp) Arrays.fill(row, 2); Map<Integer, Integer> pos = new HashMap<>(); for (int i = 0; i < n; ++i) pos.put(A[i], i); for (int j = 2; j < n; ++j) { for (int i = j-1; i > 0; --i) { int prev = A[j] - A[i]; if (prev >= A[i]) break; if (!pos.containsKey(prev)) continue; dp[i][j] = dp[pos.get(prev)][i] + 1; res = Math.max(res, dp[i][j]); } } return res; } }
Analysis:
相关文章
- LeetCode --- 58. Length of Last Word
- How to view the dependency tree of a given npm module?
- What is the purpose of FormsAuthenticationTicket isPersistent property?
- List of content management systems
- 【手记】小心在where中使用NEWID()的大坑 【手记】解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题 【C#】组件分享:FormDragger窗体拖拽器 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
- Flutter Unhandled Exception: Concurrent modification during iteration: Instance(length:3) of ‘_Growa
- Can't connect to X11 window server using ':1.0' as the value of the DISPLAY variable.
- 继承“HibernateDaoSupport”后,报“The hierarchy of the type AccoutDaoImpl is inconsistent”的解决方案
- Why am I able to change the contents of const char *ptr?
- 论文阅读:Robot Learning of Everyday Object Manipulations via Human Demonstration
- scheme :The components of a URL
- 【Vue2报错系列】解决Uncaught TypeError: Cannot read properties of undefined (reading ‘length‘)
- leetcode || 58、Length of Last Word
- Stacks of Flapjacks
- provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.
- [LeetCode] 1239. Maximum Length of a Concatenated String with Unique Characters 串联字符串的最大长度
- [LeetCode] 1161. Maximum Level Sum of a Binary Tree 最大层内元素和
- [CareerCup] 3.4 Towers of Hanoi 汉诺塔
- Solve Error Debug Assertion Failed Expression vector iterators incompatible Using PCL in Release Mode of VS2010
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
- jqGrid('setSelection',rowid)报Cannot read property 'multiple' of undefined