当前位置:网站首页>0 dynamic programming medium leetcode873. Length of the longest Fibonacci subsequence
0 dynamic programming medium leetcode873. Length of the longest Fibonacci subsequence
2022-07-28 02:32:00 【18 ARU】
873. The length of the longest Fibonacci subsequence
describe
If the sequence X_1, X_2, …, X_n Meet the following conditions , Just say it's Fibonacci Of :
n >= 3
For all i + 2 <= n, There are X_i + X_{i+1} = X_{i+2}
Given a strictly increasing array of positive integers to form a sequence arr , find arr The length of the longest fibonaccian subsequence in . If one doesn't exist , return 0 .
( Think about it , Subsequences are derived from the original sequence arr Derived from , It is from arr Delete any number of elements in the ( It's also possible to delete ), Without changing the order of the rest of the elements . for example , [3, 5, 8] yes [3, 4, 5, 6, 7, 8] A subsequence of )
Example 1:
Input : arr = [1,2,3,4,5,6,7,8]
Output : 5
explain : The longest Fibonacci subsequence is [1,2,3,5,8] .
Example 2:
Input : arr = [1,3,7,11,12,14,18]
Output : 3
explain : The longest Fibonacci subsequence is [1,11,12]、[3,11,14] as well as [7,11,18] .
Tips :
3 <= arr.length <= 1000
1 <= arr[i] < arr[i + 1] <= 10^9
source : Power button (LeetCode)
link :https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
There are three values to consider , Before fixation ( Or after ) Two keep testing the third .
Double loop considers all combinations of two numbers , Then judge whether there is a third one that meets the conditions in the following numbers , If there is , Update length
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;
}
}
边栏推荐
- 【ROS进阶篇】第九讲 基于Rviz和Arbotix控制的机器人模型运动
- pytorch优化器设置
- Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
- MySQL高可用和主从同步
- Necessary knowledge points of the original group
- Say yes, I will love you, and I will love you well
- 实际工作中,我是如何使用 Postman 做接口测试?
- Go learning 01
- Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
- Common SQL statement query
猜你喜欢

Compile and use Qwt in qt|vs2017

Unity 保存图片到相册以及权限管理

Maskedauutoencoders visual learner cvpr2022

MySQL是如何利用索引的(荣耀典藏版)

What problems should be avoided when using BigDecimal type? (glory Collection Edition)

Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report

Execute add migration migration and report build failed

【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例

Appium click operation sorting

AWS elastic three swordsman
随机推荐
探究flex-basis
retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'
Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
How MySQL uses indexes (glory Collection Edition)
Necessary knowledge points of the original group
基于stm32的恒功率无线充电
MySQL高可用和主从同步
Ceresdao: new endorsement of ventures Dao
Wechat campus bathroom reservation applet graduation design finished product (3) background function
Wechat campus bathroom reservation applet graduation design finished product (2) applet function
并发编程的三大核心问题(荣耀典藏版)
cn+dt
【HCIP】BGP 基础
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(2)小程序功能
[机缘参悟-53]:阳谋立身,阴谋防身
This operation may not be worth money, but it is worth learning | [batch cutting of pictures]
"Risking your life to upload" proe/creo product structure design - seam and buckle
Software test interview questions: common post data submission methods
CeresDAO:全球首个基于DAO赋能Web3.0的去中心化数字资产管理协议
regular expression