4130. Maximum Score After Binary Swaps
My accepted Python solution to LeetCode problem 4130, Maximum Score After Binary Swaps, running in 175ms.
- Difficulty: Medium
- Python
- Runtime 175ms
- Memory 33.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 175ms, memory 33.2MB, accepted 2026-01-02.
class Solution:
def maximumScore(self, nums: List[int], s: str) -> int:
import heapq
n = len(nums)
heap = [] # max heap (use negative values)
ans = 0
for i in range(n):
heapq.heappush(heap, -nums[i])
if s[i] == '1':
ans += -heapq.heappop(heap)
return ans