-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2555.cpp
More file actions
54 lines (49 loc) · 1.58 KB
/
2555.cpp
File metadata and controls
54 lines (49 loc) · 1.58 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 maximizeWin(vector<int>& prizePositions, int k) {
int n = prizePositions.size();
int start = prizePositions[0];
int end = prizePositions[n - 1];
if (end - start <= k * 2) return n;
vector<pair<int, int>> v;
int index = 0;
while (index < n) {
int s = index;
int pos = prizePositions[s];
while (index + 1 < n && prizePositions[index + 1] == pos) index++;
int count = index - s + 1;
v.push_back({pos, count});
index++;
}
int m = v.size();
vector<int> leftDP(m, 0);
int left = 0;
int sum = 0;
for (int right = 0; right < m; ++right) {
sum += v[right].second;
while (v[right].first - v[left].first > k) {
sum -= v[left].second;
left++;
}
if (right - 1 >= 0) leftDP[right] = max(leftDP[right - 1], sum);
else leftDP[right] = sum;
}
vector<int> rightDP(m, 0);
int r = m - 1;
sum = 0;
for (int l = m - 1; l >= 0; --l) {
sum += v[l].second;
while (v[r].first - v[l].first > k) {
sum -= v[r].second;
r--;
}
if (l + 1 <= m - 1) rightDP[l] = max(rightDP[l + 1], sum);
else rightDP[l] = sum;
}
int res = 0;
for (int i = 0; i < m - 1; ++i) {
res = max(res, leftDP[i] + rightDP[i + 1]);
}
return res;
}
};