-
Notifications
You must be signed in to change notification settings - Fork 91
/
LengthOfLongestFibonacciSubsequence873.java
178 lines (163 loc) · 5.59 KB
/
LengthOfLongestFibonacciSubsequence873.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* 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++.)
*/
public class LengthOfLongestFibonacciSubsequence873 {
public int lenLongestFibSubseq(int[] A) {
int len = A.length;
int longest = Integer.MIN_VALUE;
for (int i=0; i<len-3; i++) {
for (int j=i+1; j<len-2; j++) {
int a = A[i];
int b = A[j];
int idx = Arrays.binarySearch(A, j+1, len, a + b);
if (idx < 0) continue;
int l = 2;
while (idx >= 0 && idx < len) {
l++;
a = b;
b = A[idx];
if (idx >= len) break;
idx = Arrays.binarySearch(A, idx + 1, len, a + b);
}
if (l > longest) {
longest = l;
}
}
}
return longest == Integer.MIN_VALUE ? 0 : longest;
}
public int lenLongestFibSubseq2(int[] A) {
int N = A.length;
int[][] dp = new int[N + 1][N + 1];
int res = 0;
for (int i=2; i<N; i++) {
for (int j=i+1; j<=N; j++) {
int toBeFound = A[j-1] - A[i-1];
if (toBeFound > A[i-2]) break;
int idx = Arrays.binarySearch(A, 0, i-1, toBeFound);
if (idx >= 0) {
if (dp[idx+1][i] == 0) {
dp[i][j] = 3;
} else {
dp[i][j] += dp[idx+1][i] + 1;
}
}
if (dp[i][j] > res) res = dp[i][j];
}
}
return res;
}
public int lenLongestFibSubseq3(int[] A) {
int N = A.length;
Map<Integer, Integer> index = new HashMap();
for (int i = 0; i < N; ++i) index.put(A[i], i);
int[][] dp = new int[N + 1][N + 1];
int res = 0;
for (int i=2; i<N; i++) {
for (int j=i+1; j<=N; j++) {
int toBeFound = A[j-1] - A[i-1];
if (toBeFound > A[i-2]) break;
if (index.containsKey(toBeFound)) {
int idx = index.get(toBeFound);
if (dp[idx+1][i] == 0) {
dp[i][j] = 3;
} else {
dp[i][j] += dp[idx+1][i] + 1;
}
}
if (dp[i][j] > res) res = dp[i][j];
}
}
return res;
}
/**
* https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/solution/
*/
public int lenLongestFibSubseq4(int[] A) {
int N = A.length;
Map<Integer, Integer> index = new HashMap();
for (int i = 0; i < N; ++i)
index.put(A[i], i);
Map<Integer, Integer> longest = new HashMap();
int ans = 0;
for (int k = 0; k < N; ++k) {
for (int j = 0; j < k; ++j) {
int i = index.getOrDefault(A[k] - A[j], -1);
if (i >= 0 && i < j) {
// Encoding tuple (i, j) as integer (i * N + j)
int cand = longest.getOrDefault(i * N + j, 2) + 1;
longest.put(j * N + k, cand);
ans = Math.max(ans, cand);
}
}
}
return ans >= 3 ? ans : 0;
}
/**
* https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/discuss/152343/C++JavaPython-Check-Pair
*/
public int lenLongestFibSubseq5(int[] A) {
Set<Integer> s = new HashSet<Integer>();
for (int x : A) s.add(x);
int res = 2;
for (int i = 0; i < A.length; ++i) {
for (int j = i + 1; j < A.length; ++j) {
int a = A[i], b = A[j], l = 2;
while (s.contains(a + b)) {
b = a + b;
a = b - a;
l++;
}
res = Math.max(res, l);
}
}
return res > 2 ? res : 0;
}
/**
* https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/discuss/152343/C++JavaPython-Check-Pair
*/
public int lenLongestFibSubseq6(int[] A) {
int res = 0;
int[][] dp = new int[A.length][A.length];
Map<Integer, Integer> index = new HashMap<>();
for (int j = 0; j < A.length; j++) {
index.put(A[j], j);
for (int i = 0; i < j; i++) {
int k = index.getOrDefault(A[j] - A[i], -1);
dp[i][j] = (A[j] - A[i] < A[i] && k >= 0) ? dp[k][i] + 1 : 2;
res = Math.max(res, dp[i][j]);
}
}
return res > 2 ? res : 0;
}
}