LeetCode 3781 - Maximum Score After Binary Swaps

Before I write the full guide, I need one clarification: can you confirm the exact LeetCode 3781 problem details or provide the official title/link?

LeetCode Problem 3781

Difficulty: 🟡 Medium
Topics: Array, String, Greedy, Heap (Priority Queue)

Solution

Before I write the full guide, I need one clarification: can you confirm the exact LeetCode 3781 problem details or provide the official title/link? “Maximum Score After Binary Swaps” does not match a problem I can reliably identify from memory, and I want to ensure the algorithm and proof are correct.

In particular, I want to verify this interpretation:

  • You may repeatedly swap only adjacent "01" -> "10" patterns, meaning '1' bits can move only to the left, never right.
  • The number of '1' characters stays fixed.
  • The goal is to maximize the sum of nums[i] at positions containing '1' after any sequence of valid swaps.

If that interpretation is correct, I will provide the complete detailed guide in the exact format you requested, including:

  • Problem Understanding
  • Brute Force and Optimal Approaches
  • Comparison table
  • Step by step Algorithm Walkthrough with proof sketch
  • Complete Python solution
  • Complete Go solution
  • Worked examples
  • Complexity analysis
  • Assert based test cases
  • Edge cases

I am asking because the optimal strategy depends on a subtle invariant about how far each '1' can move, and I want to avoid giving an incorrect derivation.