English version
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 <= n
Given a strictly increasing array a positive integers forming a sequence, find the length of the long EST fibonacci-like subsequence of A. If One does not exist, return 0.
(Recall that a subsequence was 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 is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence 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 + +.)
Chinese version:
Give you a strictly monotonically incrementing array, what is the length of the longest Fibonacci sequence in the array? For example, if the input array is [1, 2, 3, 4, 5, 6, 7, 8], because the longest Fibonacci sequence is 1, 2, 3, 5, 8, the output should be 5.
Analysis:
Idea One
In the Fibonacci sequence, the nth number is equal to the sum of the number of n-1 and the number of the first n-2.
Consider the length of the longest Fibonacci sequence ending with the first I number in the array (A[i]). For each j (0 <= J < i), A[j] may be a number preceding the a[i in a Fibonacci sequence. If there is a K (K < J) satisfying A[k] + a[j] = A[i], then these three numbers form a Fibonacci sequence. This Fibonacci sequence with A[i] as the end and the previous number is A[J] Adds a number a[i on the basis of the sequence of a[j] and the previous number is a[k], so the length of the former is based on the length of the latter plus 1.
We can use a two-dimensional array lengths to record the length of the Fibonacci sequence. The length of the Fibonacci sequence in a two-dimensional array where the first number is a[j] ends with a[i in the input array, and the number in column J of row I is the meaning of the numbers. If there is a number k, satisfies a[k] + a[j] = A[i], then lengths[i][j] = Lengths[j][k] + 1. If there is no k that satisfies the condition, it means that this a[j], A[i] is not in any Fibonacci sequence, lengths[i][j] equals 2.
The maximum value in a two-dimensional array lengths is the output value.
1 classSolution {2 Public intLenlongestfibsubseq (int[] A) {3 if(NULL= = A | | A.length = = 0) {4 return0;5 }6Map<integer, integer> map =NewHashmap<>();7 for(inti = 0; i < a.length; i + +) {8 Map.put (A[i], i);9 }Ten One int[] lengths =New int[a.length][a.length]; A intMaxLength = 1; - for(inti = 1; i < a.length; i + +) { - intNum_3 =A[i]; the intLength = 2; - for(intj = i-1; J >= 0; J--) { - intNum_2 =A[j]; - intNum_1 = Num_3-num_2; + - intLen = 2; + if(Num_1 < num_2 &&Map.containskey (num_1)) { ALen = Lengths[j][map.get (num_1)] + 1; at } -LENGTHS[I][J] =Len; -Length =Math.max (length, Len); - } -MaxLength =Math.max (maxLength, length); - } in returnMaxLength > 2? maxlength:0; - } to}
Two ideas
Double Loop enumeration for all possible scenarios
1 classSolution {2 Public intLenlongestfibsubseq (int[] A) {3 intN =a.length;4Set<integer> S =NewHashSet ();5 for(intx:a) S.add (x);6 7 intAns = 0;8 for(inti = 0; i < N; ++i)9 for(intj = i+1; J < N; ++j) {Ten intx = A[j], y = a[i] +A[j]; One intLength = 2; A while(S.contains (y)) { - //x, y, y, x+y - intTMP =y; theY + =x; -x =tmp; -Ans = math.max (ans, + +length); - } + } - + returnAns >= 3? ans:0; A } at}
The longest Fibonacci sequence-leetcode-873