-
Notifications
You must be signed in to change notification settings - Fork 46
/
_2555.cpp
33 lines (30 loc) · 815 Bytes
/
_2555.cpp
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
/**
* LeetCode 2555 - Maximize Win From Two Segments
*
* Sliding window + Prefix/Suffix max
*/
class Solution {
public:
int maximizeWin(vector<int>& prize, int k) {
int n = prize.size();
if (n == 1)
return 1;
vector<int> f(n), g(n);
for (int i=0, pre=0; i<n; i++) {
while (pre < i && prize[i] - prize[pre] > k)
pre++;
f[i] = i - pre + 1;
}
for (int i=n-1, pre=n-1; i>=0; i--) {
while (pre > i && prize[pre] - prize[i] > k)
pre--;
g[i] = pre - i + 1;
if (i + 1 < n)
g[i] = max(g[i], g[i+1]);
}
int ans = 0;
for (int i=0; i<n-1; i++)
ans = max(ans, f[i] + g[i+1]);
return ans;
}
};