1. 程式人生 > >最長斐波那契序列-LeetCode-873

最長斐波那契序列-LeetCode-873

get reduce without rem rmi ons 增加 derived 循環

英文版
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 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++.)

中文版:
給你一個嚴格單調遞增的數組,請問數組裏最長的斐波那契序列的長度是多少?例如,如果輸入的數組是[1, 2, 3, 4, 5, 6, 7, 8],由於其中最長的斐波那契序列是1, 2, 3, 5, 8,因此輸出應該是5。

分析:

思路一
在斐波那契序列中,第n個數字等於第n-1個數字與第n-2個數字之和。

考慮以數組中第i個數字(記為A[i])為結尾的最長斐波那契序列的長度。對於每一個j(0 <= j < i),A[j]都有可能是在某個斐波那契序列中A[i]前面的一個數字。如果存在一個k(k < j)滿足A[k] + A[j] = A[i],那麽這三個數字就組成了一個斐波那契序列。這個以A[i]為結尾、前一個數字是A[j]的斐波那契序列是在以A[j]為結尾、前一個數字是A[k]的序列的基礎上增加了一個數字A[i],因此前者的長度是在後者的長度基礎上加1。


我們可以用一個二維數組lengths來記錄斐波那契序列的長度。二維數組中第i行第j列數字的含義是以輸入數組中A[i]結尾、並且前一個數字是A[j]的斐波那契序列的長度。如果存在一個數字k,滿足A[k] + A[j] = A[i],那麽lengths[i][j] = lengths[j][k] + 1。如果不存在滿足條件的k,那麽意味這A[j]、A[i]不在任意一個斐波那契序列中,lengths[i][j]等於2。

二維數組lengths中的最大值就是輸出值。

 1 class Solution {
 2     public int lenLongestFibSubseq(int[] A) {
 3         if (null == A || A.length == 0) {
 4             return 0;
 5         }
 6         Map<Integer, Integer> map = new HashMap<>();
 7         for (int i = 0; i < A.length; i ++) {
 8             map.put(A[i], i);
 9         }
10         
11         int[][] lengths = new int[A.length][A.length];
12         int maxLength = 1;
13         for (int i = 1; i < A.length; i ++) {
14             int num_3 = A[i];
15             int length = 2;
16             for (int j = i-1; j >= 0; j --) {
17                 int num_2 = A[j];
18                 int num_1 = num_3 - num_2;
19                 
20                 int len = 2;
21                 if (num_1 < num_2 && map.containsKey(num_1)) {
22                     len = lengths[j][map.get(num_1)] + 1;
23                 }
24                 lengths[i][j] = len;
25                 length = Math.max(length, len);
26             }
27             maxLength = Math.max(maxLength, length);
28         }
29         return maxLength > 2 ? maxLength : 0;
30     }
31 }

思路二
雙重循環枚舉所有可能的情況

 1 class Solution {
 2     public int lenLongestFibSubseq(int[] A) {
 3         int N = A.length;
 4         Set<Integer> S = new HashSet();
 5         for (int x: A) S.add(x);
 6 
 7         int ans = 0;
 8         for (int i = 0; i < N; ++i)
 9             for (int j = i+1; j < N; ++j) {
10                 int x = A[j], y = A[i] + A[j];
11                 int length = 2;
12                 while (S.contains(y)) {
13                     // x, y -> y, x+y
14                     int tmp = y;
15                     y += x;
16                     x = tmp;
17                     ans = Math.max(ans, ++length);
18                 }
19             }
20 
21         return ans >= 3 ? ans : 0;
22     }
23 }

最長斐波那契序列-LeetCode-873