-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1027.cpp
More file actions
54 lines (43 loc) · 1.47 KB
/
Copy path1027.cpp
File metadata and controls
54 lines (43 loc) · 1.47 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
52
53
54
class Solution {
public:
int longestArithSeqLength(vector<int>& A) {
int n = A.size();
if(n==0 || n==1 || n==2)return n;
//sort(A.begin(), A.end());
int ma = 0;
for(int i = 0; i < n; ++i)
if(A[i] > ma)ma = A[i];
vector< vector<int> > b(ma+1);
//b.resize(ma+1);
for(int i = 0; i < n; ++i)
b[A[i]].push_back(i);
//反着推吧,i比j大
int** dp = new int*[n];
for(int i = 0; i < n; ++i)
{
dp[i] = new int[n];
}
int best = 2;
for(int i = 0; i < n; ++i)
{
for(int j = i-1; j < i && j >= 0; --j)
{
dp[i][j] = 2;
int c = A[i] - A[j];
int t = A[j] - c;
//传入的数组数值不能是负数啊
//开始时没后边你的条件
if(t < 0 || t > ma)continue;
for(int l = 0; l < b[t].size(); ++l)
{
if(b[t][l] > j)continue;
if(dp[j][b[t][l]] + 1 > dp[i][j])
dp[i][j] = dp[j][b[t][l]] + 1;
}
if(dp[i][j] > best)best = dp[i][j];
//cout << i << "|" << j << "|" << dp[i][j] << endl;
}
}
return best;
}
};