LC 873. Length of Longest Fibonacci Subsequence
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 of A
. 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) from A
, 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++.)
my solution.
Runtime: 935 ms, faster than 2.29% of Java online submissions for Length of Longest Fibonacci Subsequence.
我的思路,采用map,虽然也是dp,但是还是一个n2的时间复杂度,看了别人的做法,用的是头尾指针逼近,二分法。今天的周赛里判断数组成三角形的快速判断方法也是这样的。
class Solution {
public int lenLongestFibSubseq(int[] A) {
Map<Integer, Map<Integer,Integer>> mp = new HashMap<>();
int ret = ;
for(int i=; i<A.length; i++){
if(i == ) mp.put(A[i], new HashMap<>());
else {
mp.put(A[i],new HashMap<>());
Map<Integer,Integer> mpai = mp.get(A[i]);
for(int j=; j<i; j++){
Map<Integer,Integer> mpaj = mp.get(A[j]);
mpai.put(A[j], mpaj.getOrDefault(A[i]-A[j],)+);
ret = Math.max(ret, mpai.get(A[j]));
}
}
}
return ret > ? ret + : ;
}
}
网上的思路。
Runtime: 41 ms, faster than 94.29% of Java online submissions for Length of Longest Fibonacci Subsequence.
class Solution {
public int lenLongestFibSubseq(int[] A) {
if(A.length < ) return ;
int ret = ;
int[][] dp = new int[A.length][A.length];
for(int i=; i<A.length; i++){
int left = , right = i-;
while(left < right){
if(A[left] + A[right] == A[i]){
dp[right][i] = dp[left][right]+;
ret = Math.max(ret, dp[right][i]);
left++;
right--;
}else if(A[left] + A[right] > A[i]){
right--;
}else {
left++;
}
}
}
return ret == ? : ret + ;
}
}
LC 873. Length of Longest Fibonacci Subsequence的更多相关文章
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- LeetCode 873. Length of Longest Fibonacci Subsequence
原题链接在这里:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/ 题目: A sequence X_1, X ...
- 873. Length of Longest Fibonacci Subsequence
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...
- [LeetCode] Length of Longest Fibonacci Subsequence 最长的斐波那契序列长度
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...
- [Swift]LeetCode873. 最长的斐波那契子序列的长度 | Length of Longest Fibonacci Subsequence
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...
- [LC] 300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- 2.SpringMVC执行流程
SpringMVC 执行流程: 执行流程简单分析: 1.浏览器提交请求到中央调度器 2.中央调度器直接将请求转给处理器映射器 3.处理器映射器会根据请求,找到处理该请求的处理器,并将其封装为处理器执行 ...
- PCQQ - 发送自定义的XML卡片消息
效果: 原理: qq分享产生的xml卡片消息存储在qq内存中,可以在qq运行内存中搜索找到其xml源码,记录源码相应的内存地址,通过内存地址修改掉内存数据,再次转发这条分享的消息就会发现内容的变化. ...
- 01.Zabbix监控快速入门
1.监控知识基本概述 1.为什么要使用监控 1.对系统不间断实时监控 2.实时反馈系统当前状态 3.保证服务可靠性安全性 4.保证业务持续稳定运行 2.如何进行监控,比如我们需要监控磁盘的使用率 1. ...
- STM32L1xx——ADC(中断/DMA)样例代码
此代码欲实现的功能是:使用中断或者DMA的方式采集滑动变阻器采集到的电压值,使用单ADC单通道采样! (由于不是直接需要电压,所以转换函数我就没列出来,可根据自身需要去网上查到转换的函数.) 代码结构 ...
- linux基础3-磁盘和文件系统相关
一 dumpe2fs : 在Linux使用过程中,我们如果要了解文件系统的配置情况,可以使用dumpe2fs查看ext2/ext3/ext4格式的文件系统信息. 命令格式: dumpe2fs [选项] ...
- EasyUI中取的DataGrid中选中行数据
dataGrid中显示列:ItemID,ItemCode,ItemName,Note 一.选中一行 var selRow = $('#dataGrid').datagrid('getSelected' ...
- springcloud——学习
构建分布式系统 配置管理 服务发现 断路器.路由.微代理.事件总线 全局锁.决策竞选. springCloud是一个微服务框架, 提供多种功能. 底层封装的HttpClinet springCloud ...
- Python3+Appium学习笔记01-环境配置(上)
公司可能也有关于对app自动化的一些想法,让我去研究下.当然以移动互联网的热度.对于app自动化测试技术听闻已久.也一直想要去学习.正好.这次可以在工作时间中学习.emmm.希望自己能坚持把这个系列更 ...
- Python3学习笔记37-LeetCode刷题
LeetCode中国官网一个用来刷编程题的网站,收录了很多面试题.感觉还是学习到很多.记录一下思路.代码还是要多敲. 建议编写完后直接在LeetCode上运行和提交.提交时会有不同的测试用例来测试代码 ...
- 五分钟彻底搞懂你一直没明白的Linux内存管理
现在的服务器大部分都是运行在Linux上面的,所以,作为一个程序员有必要简单地了解一下系统是如何运行的.对于内存部分需要知道: 地址映射 内存管理的方式 缺页异常 先来看一些基本的知识,在进程看来,内 ...