Skip to content

Commit 10dc542

Browse files
authored
Create maximize-win-from-two-segments.py
1 parent 36353a3 commit 10dc542

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(object):
6+
def maximizeWin(self, prizePositions, k):
7+
"""
8+
:type prizePositions: List[int]
9+
:type k: int
10+
:rtype: int
11+
"""
12+
dp = [0]*(len(prizePositions)+1)
13+
result = left = 0
14+
for right in xrange(len(prizePositions)):
15+
while prizePositions[right]-prizePositions[left] > k:
16+
left += 1
17+
dp[right+1] = max(dp[right], right-left+1)
18+
result = max(result, dp[left]+(right-left+1))
19+
return result

0 commit comments

Comments
 (0)