Skip to content

Commit 36353a3

Browse files
authored
Create maximize-win-from-two-segments.cpp
1 parent a26efef commit 36353a3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// two pointers, sliding window, dp
5+
class Solution {
6+
public:
7+
int maximizeWin(vector<int>& prizePositions, int k) {
8+
vector<int> dp(size(prizePositions) + 1);
9+
int result = 0;
10+
for (int right = 0, left = 0; right < size(prizePositions); ++right) {
11+
while (prizePositions[right] - prizePositions[left] > k) {
12+
++left;
13+
}
14+
dp[right + 1] = max(dp[right], right - left + 1);
15+
result = max(result, dp[left] + (right - left + 1));
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)