LeetCode 873. Length of Longest Fibonacci Subsequence
原题链接在这里:https://leetcode.com/problems/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].
题解:
Take a = A[i] and b = A[j] as first 2 elements, j>i.
Check if A contains A[i]+A[j]. If yes, then a = b, b = a+b, and fibonacci sequence length ++.
Until A doesn't contian (A[i] + A[j]), update the global longest length.
Time Complexity: O(n^2 * logM). n = A.length. M is the largest number in A, since in while loop it grows exponentially, while takes logM.
Space: O(n).
AC Java:
class Solution {
public int lenLongestFibSubseq(int[] A) {
if(A == null || A.length < 3){
return 0;
} HashSet<Integer> hs = new HashSet<>();
for(int a : A){
hs.add(a);
} int res = 0; int n = A.length;
for(int i = 0; i<n-2; i++){
for(int j = i+1; j<n-1; j++){
int a = A[i];
int b = A[j];
int l = 2;
while(hs.contains(a+b)){
int temp = a;
a = b;
b = temp+b;
l++;
} res = Math.max(res, l);
}
} return res > 2 ? res : 0;
}
}
LeetCode 873. Length of Longest Fibonacci Subsequence的更多相关文章
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 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 all i + 2 ...
- 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 ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode&Python] Problem 594. Longest Harmonious Subsequence
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
随机推荐
- C++冒泡排序及优化
冒泡排序 1.经典冒泡排序 经典的冒泡排序为从左边开始依次判断排序,每次最终仅将一个数向后冒泡,而对于其他数的排序没有什么帮助:如果已经所有元素已经是有序的,依然执行循环. 2.优化冒泡排序 优化地方 ...
- BJFU-217-基于链式存储结构的图书信息表的逆序存储
这道题可以用头插法创建列表,然后正常输出: #include<stdio.h> #include<stdlib.h> #define MAX 100 typedef struc ...
- layui 上传图片 实现过程
layui.user一个页面只能有一个,写多了会实现js效果 上传图片官方文档有很多功能,但是演示的代码只是一个一个功能演示,如果要综合起来js代码不是简单的拼凑,需要放在指定位置,比如下面的限制文件 ...
- C语言的变参列表 va_list
1. va_list的基本原理和用法 #include<stdio.h> #include<stdarg.h> void func(int i,char *ch,...){ / ...
- redis源码分析(五)--cluster(集群)结构
Redis集群 Redis支持集群模式,集群中可以存在多个master,每个master又可以拥有多个slave.数据根据关键字映射到不同的slot,每一个master负责一部分的slots,数据被存 ...
- go switch 和java C#有不同
1 switch 后的语句可以有简单的赋值语句 2 case :后的语句结束后不需要break;默认自动结束 除非以 fallthrough 语句结束,否则分支会自动终止 没有条件的 switch 有 ...
- 关于vuecli的一些问题
在vue打包之后,我们引入的css路径和js路径会变成绝对路径 需要在vue.config.js里面设置publicpath为"./" 同时在做前后端分离开发时,我们通常会用到ax ...
- python3 简陋的学生信息管理系统
# 编写一个“学生信息管理系统”# 输入序号:1. 输入学生信息,学生信息包括:id,name,age,gender(用什么数据类型保存?)# 2. 查询:输入学生姓名和id,显示学生个人信息# 3. ...
- Hive 系列(三)—— Hive CLI 和 Beeline 命令行的基本使用
一.Hive CLI 1.1 Help 使用 hive -H 或者 hive --help 命令可以查看所有命令的帮助,显示如下: usage: hive -d,--define <key=va ...
- C#服务器全面讲解与制作
C#服务器全面讲解与制作一 环境配置与基础架构 环境配置 基础的服务器架构 这里我会讲解高级的C#服务器的全面制作流程 会对大家有很大的帮助 不过在这个教程中主要是讲解服务器的制作,所以不会讲解客户端 ...