Skip to content

Commit a629711

Browse files
Time: 998 ms (5.00%) | Memory: 35.3 MB (24.23%) - LeetSync
1 parent 90263e4 commit a629711

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from sortedcontainers import SortedList
2+
3+
class Solution:
4+
def constrainedSubsetSum(self, nums: List[int], k: int) -> int:
5+
avl = SortedList([nums[0]])
6+
7+
for i in range(1, len(nums)):
8+
nums[i] = max(nums[i], avl[-1] + nums[i])
9+
10+
avl.add(nums[i])
11+
12+
if len(avl) > k:
13+
avl.remove(nums[i - k])
14+
15+
return max(nums)

0 commit comments

Comments
 (0)