-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestArithmeticSubsequence.cpp
More file actions
51 lines (49 loc) · 1.43 KB
/
longestArithmeticSubsequence.cpp
File metadata and controls
51 lines (49 loc) · 1.43 KB
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
// Source: https://leetcode.com/problems/longest-arithmetic-subsequence/
// Author: Miao Zhang
// Date: 2021-04-05
class Solution {
public:
int longestArithSeqLength(vector<int>& A) {
int n = A.size();
unordered_map<int, vector<int>> h;
for (int i = 0; i < n; i++) {
h[A[i]].push_back(i);
}
int res = 2;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int d = A[j] - A[i];
int l = 2;
int idx = j;
int t = A[j] + d;
while (h.count(t)) {
auto it = upper_bound(begin(h[t]), end(h[t]), idx);
if (it == end(h[t])) break;
idx = *it;
t += d;
l++;
}
res = max(res, l);
}
}
return res;
}
};
class Solution {
public:
int longestArithSeqLength(vector<int>& A) {
vector<short> a(begin(A), end(A));
int n = A.size();
const short h = *max_element(begin(a), end(a));
vector<vector<short>> dp(n, vector<short>(2 * h + 1, 1));
short res = 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
short d = a[i] - a[j] + h;
dp[i][d] = dp[j][d] + 1;
res = max(res, dp[i][d]);
}
}
return res;
}
};