当前位置:网站首页>0动态规划中等 LeetCode873. 最长的斐波那契子序列的长度
0动态规划中等 LeetCode873. 最长的斐波那契子序列的长度
2022-07-28 01:06:00 【18阿鲁】
描述
如果序列 X_1, X_2, …, X_n 满足下列条件,就说它是 斐波那契式 的:
n >= 3
对于所有 i + 2 <= n,都有 X_i + X_{i+1} = X_{i+2}
给定一个严格递增的正整数数组形成序列 arr ,找到 arr 中最长的斐波那契式的子序列的长度。如果一个不存在,返回 0 。
(回想一下,子序列是从原序列 arr 中派生出来的,它从 arr 中删掉任意数量的元素(也可以不删),而不改变其余元素的顺序。例如, [3, 5, 8] 是 [3, 4, 5, 6, 7, 8] 的一个子序列)
示例 1:
输入: arr = [1,2,3,4,5,6,7,8]
输出: 5
解释: 最长的斐波那契式子序列为 [1,2,3,5,8] 。
示例 2:
输入: arr = [1,3,7,11,12,14,18]
输出: 3
解释: 最长的斐波那契式子序列有 [1,11,12]、[3,11,14] 以及 [7,11,18] 。
提示:
3 <= arr.length <= 1000
1 <= arr[i] < arr[i + 1] <= 10^9
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
分析
需要考虑的数值有三个,固定前(或后)两个不断试探第三个。
双循环考虑所有的两个数的组合,然后判断后面的数字中是否存在满足条件的第三个,如果存在,更新长度
class Solution {
public int lenLongestFibSubseq(int[] arr) {
int n = arr.length;
int[][] dp = new int[n][n];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(arr[i],i);
}
int ans = 0;
for (int i = 0; i <n; i++) {
for (int j = i + 1; j < n; j++) {
int c = arr[i]+arr[j];
if (map.containsKey(c)) {
dp[j][map.get(c)] = dp[i][j] == 0 ? 3 : dp[i][j] + 1;
ans = Math.max(ans,dp[j][map.get(c)]);
}
}
}
return ans;
}
}
边栏推荐
- MySQL pymysql operation
- Interviewer: what is the factory method mode?
- [Star Project] small hat aircraft War (VI)
- Leetcode hot topic Hot 100 - > 1. Sum of two numbers
- Shell regular and metacharacters
- Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters
- Network must know topics
- cn+dt
- Use of Day6 functions and modules
- MySQL high availability and master-slave synchronization
猜你喜欢
![[Yugong series] July 2022 go teaching course 019 - for circular structure](/img/40/b4e673de0462c3dd6ca8b8fb513914.png)
[Yugong series] July 2022 go teaching course 019 - for circular structure

这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】

Flume(5个demo轻松入门)

Under the new retail format, retail e-commerce RPA helps reshape growth

重要安排-DX12引擎开发课程后续直播将在B站进行

Promise from getting started to mastering (Chapter 3: customize (handwriting) promise)

Product axure9 English version, using repeater repeater repeater to realize multi-choice and single choice

Ceresdao: new endorsement of ventures Dao

Unity saves pictures to albums and rights management

What problems should be avoided when using BigDecimal type? (glory Collection Edition)
随机推荐
Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements
QGIS mapping: vector data mapping process and export
Feign calls get and post records
MySQL high availability and master-slave synchronization
In practical work, how do I use postman for interface testing?
Use of Day6 functions and modules
C#引入WINAPI传递中文字符串参数字符集问题
Flask1.1.4 werkzeug1.0.1 source code analysis: Blueprint
Class notes (5) (1) - 593. Binary search
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
Implementation of mongodb/mongotemplate.upsert batch inserting update data
OBS键盘插件自定义diy
Wechat campus bathroom reservation applet graduation design finished product (2) applet function
Principle and implementation of focal loss
小米网站主页面大模块——小模块+导航(浮动案例)
【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例
cn+dt
Deep understanding of recursion
[Star Project] small hat aircraft War (VI)
11 Django basics database operation