Skip to content

Commit a7f78fa

Browse files
committed
Time: 212 ms (17.99%), Space: 33.4 MB (47.44%) - LeetHub
1 parent 1c83c2d commit a7f78fa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# time complexity: O(nlogn)
2+
# space complexity: O(n)
3+
from typing import List
4+
import heapq
5+
6+
7+
class Solution:
8+
def maximumScore(self, nums: List[int], s: str) -> int:
9+
n = len(nums)
10+
total = sum(nums)
11+
cap = 0
12+
maxHeap = []
13+
zeroSum = 0
14+
15+
for i in range(n):
16+
if s[i] == '0':
17+
cap += 1
18+
zeroSum += nums[i]
19+
heapq.heappush(maxHeap, -nums[i])
20+
21+
if len(maxHeap) > cap:
22+
removed = -heapq.heappop(maxHeap)
23+
zeroSum -= removed
24+
return total - zeroSum
25+
26+
27+
nums = [2, 1, 5, 2, 3]
28+
s = "01010"
29+
print(Solution().maximumScore(nums, s))
30+
nums = [4, 7, 2, 9]
31+
s = "0000"
32+
print(Solution().maximumScore(nums, s))

0 commit comments

Comments
 (0)