-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3202.cpp
More file actions
31 lines (28 loc) · 764 Bytes
/
3202.cpp
File metadata and controls
31 lines (28 loc) · 764 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
25
26
27
28
29
30
class Solution {
public:
int maximumLength(vector<int>& nums, int k) {
int dp[1001][1001]; // prev; curr
int temp[1001];
int n = nums.size();
int res = 0;
for (int i = 0; i < k; ++i) {
for (int j = 0; j < k; ++j) {
dp[i][j] = 0;
}
}
for (int j = 0; j < k; ++j) {
dp[j][nums[0] % k] = 1;
}
for (int i = 1; i < n; ++i) {
int curr = nums[i] % k;
for (int j = 0; j < k; ++j) {
temp[j] = dp[curr][j];
}
for (int j = 0; j < k; ++j) {
dp[j][curr] = temp[j] + 1;
res = max(res, dp[j][curr]);
}
}
return res;
}
};