-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1027.cpp
More file actions
44 lines (43 loc) · 1.2 KB
/
1027.cpp
File metadata and controls
44 lines (43 loc) · 1.2 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
class Solution {
public:
int longestArithSeqLength(vector<int>& A) {//O(n3)
int n = A.size();
if(n<2)
return n;
int res = 2;
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
int diff = A[j] - A[i];
int next = A[j] + diff;
int curr = 2;
for(int k=j+1; k<n; k++){
if(A[k]==next){
next += diff;
curr++;
}
}
res = max(res,curr);
}
}
return res;
}
};
class Solution {
public:
int longestArithSeqLength(vector<int>& A) {
int n = A.size();
if(n<= 2)
return n;
int res = 2;
vector<unordered_map<int, int>> dp(n);
for(int i = 1; i < n; i++)
for(int j = 0; j < i; j++) {
int diff = (A[i] - A[j]);
int curr = 1 + (dp[j].count(diff) ? dp[j][diff] : 0);
int global_max = dp[i].count(diff) ? dp[i][diff] : 2;
dp[i][diff] = max(global_max, curr);
res = max(res, dp[i][diff]);
}
return res;
}
};