-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3409.cpp
More file actions
24 lines (24 loc) · 769 Bytes
/
3409.cpp
File metadata and controls
24 lines (24 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int longestSubsequence(vector<int>& nums) {
vector<vector<int>> dp(301, vector<int>(301, 0));
int n = nums.size();
for (int i = n - 1; i >= 0; --i) {
int num = nums[i];
for (int next = 1; next <= 300; ++next) {
int diff = abs(next - num);
dp[num][diff] = max(dp[num][diff], dp[next][diff] + 1);
}
for (int diff = 1; diff <= 300; ++diff) {
dp[num][diff] = max(dp[num][diff], dp[num][diff - 1]);
}
}
int res = 0;
for (int i = 0; i <= 300; ++i) {
for (int j = 0; j <= 300; ++j) {
res = max(res, dp[i][j]);
}
}
return res;
}
};