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, ...
随机推荐
- 【Struts2】 国际化
一.概述 二.Struts2中国际化: 2.1 问题1 全局 局部 2.2 问题2 2.3 问题3 2.4 问题4 在Action中怎样使用 在JSP页面上怎样使用 一.概述 同一款软件 可以为不同用 ...
- JavaSpring【六、AOP的API】
AOP API Spring1.2历史用法,现在仍然支持 现在xml配置和注解的用法是基于API的,只是比较简便
- 基础网络之EfficientNet
摘要: 一般情况下,我们都会根据当前的硬件资源来设计相应的卷积神经网络,如果资源升级,可以将模型结构放大以获取更好精度.我们系统地研究模型缩放并验证网络深度,宽度和分辨率之间的平衡以得到更好的性能表现 ...
- (备忘)Window7下安装Python2.6及Django1.4
1.Python2.6安装 1.1 Python2.6的下载及安装 Python 版本:2.6 下载地址:http://www.python.org/download/releases/2.6.1/ ...
- Django_02_创建模型
一:ORM简介 ORM,全拼Object-Relation Mapping,中文意为对象-关系映射,是随着面向对象的软件开发方法发展而产生的. 面向对象的开发方法是当今企业级应用开发环境中的主流开发方 ...
- linux和unix下crontab的使用
在LINUX中,周期执行的任务一般由cron这个守护进程来处理 [ps -ef | grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为 ...
- PHP配置文件(php.ini)详解
[PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...
- 编辑器 --- Visual Studio Code 英文界面转换成中文(简体)
打开编辑器 同时按下Ctrl+Shift+P打开命令面板: 之后输入"config"筛选可用命令表,最后选择配置语言命令进行选择或安装插件
- metal docs--Synchronization&memory management
https://developer.apple.com/documentation/metal/heaps/image_filter_graph_with_heaps_and_fences?langu ...
- ubuntu 16.04中文输入法安装
转自: http://blog.csdn.net/u011795345/article/details/53041707 最近刚给笔记本装了Ubuntu+win10双系统,但是ubuntu16.04没 ...