LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 175ms, memory 33.2MB, accepted 2026-01-02.

python
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

Source