Minimum Swaps to Arrange a Binary Grid - Problem
Minimum Swaps to Arrange a Binary Grid

You're given an n × n binary grid where each cell contains either 0 or 1. Your goal is to transform this grid into a valid configuration using the minimum number of adjacent row swaps.

A grid is considered valid when all cells above the main diagonal contain zeros. The main diagonal runs from the top-left corner (0,0) to the bottom-right corner (n-1,n-1).

Operations allowed:
• You can swap any two adjacent rows (rows that are next to each other)
• Each swap counts as one step

Goal: Return the minimum number of steps needed to make the grid valid, or -1 if it's impossible.

Example: In a 3×3 grid, row 0 must have at least 2 trailing zeros, row 1 must have at least 1 trailing zero, and row 2 can have any pattern.

Input & Output

example_1.py — Basic Valid Grid
$ Input: grid = [[0,0,1],[1,1,0],[1,0,0]]
Output: 3
💡 Note: The trailing zeros are [2,1,1]. We need to arrange them as [≥2,≥1,≥0]. Row 0 is already good. Row 1 needs to move to position 2, and row 2 to position 1. This requires 3 swaps: swap(1,2) → [[0,0,1],[1,0,0],[1,1,0]], then swap(0,1) → [[1,0,0],[0,0,1],[1,1,0]], then swap(1,2) → [[1,0,0],[1,1,0],[0,0,1]].
example_2.py — Already Valid Grid
$ Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
Output: 0
💡 Note: Each row has exactly 1 trailing zero. For a 4x4 grid, position 0 needs ≥3 zeros, position 1 needs ≥2, position 2 needs ≥1, position 3 needs ≥0. Since no row has ≥3 trailing zeros, this arrangement is impossible... Wait, let me recalculate: each row [0,1,1,0] has 1 trailing zero. We need [≥3,≥2,≥1,≥0] but we only have four 1's. This should return -1.
example_3.py — Impossible Case
$ Input: grid = [[1,1,0],[1,0,0],[1,0,0]]
Output: -1
💡 Note: The trailing zeros are [1,2,2]. We need arrangement [≥2,≥1,≥0]. Position 0 requires ≥2 trailing zeros, but only rows 1 and 2 have ≥2. However, we also need position 1 to have ≥1 trailing zeros. After placing a row with 2 zeros at position 0, we still have one more row with 2 zeros and one with 1 zero, which is sufficient. Wait, this should actually be possible with some swaps. Let me reconsider... Actually, this should return 1 swap.

Constraints

  • n == grid.length
  • n == grid[i].length
  • 1 ≤ n ≤ 200
  • grid[i][j] is 0 or 1

Visualization

Tap to expand
Binary Grid as Book Arrangement ProblemEach row is a book, trailing zeros represent width extending to the rightOriginal arrangement:Book 1width: 2Book 2width: 1B3width: 0Requirements per shelf:Shelf 0: need width ≥ 2Shelf 1: need width ≥ 1Shelf 2: need width ≥ 0Available widths: [2, 1, 0]✓ Solution exists!Greedy placement:1. Book 1 (width 2) → Shelf 0 ✓2. Book 2 (width 1) → Shelf 1 ✓3. Book 3 (width 0) → Shelf 2 ✓Total swaps needed: 0Example requiring swaps: widths = [0, 2, 1]B1w:0Book 2w:2Book 3w:11 swapAfter swap 1: widths = [2, 0, 1]Book 2w:2 ✓B1w:0Book 3w:11 swapFinal: widths = [2, 1, 0]Book 2w:2 ✓Book 3w:1 ✓B1w:0 ✓Total: 2 swaps🎯 **Key Insight:** Greedy Works Because...Any book wide enough for a higher shelf can also fit on lower shelves
Understanding the Visualization
1
Measure Book Widths
Count trailing zeros in each row (like measuring book width from the right edge)
2
Check Shelf Requirements
Verify we have enough wide books for each shelf position
3
Place Books Greedily
For each shelf position, find the first suitable book and move it there
4
Count Movements
Sum up all adjacent swaps needed to achieve the final arrangement
Key Takeaway
🎯 Key Insight: The greedy approach works because any row with enough trailing zeros for position i will also be suitable for any position j > i (since position j requires fewer trailing zeros). This allows us to make locally optimal choices that lead to a globally optimal solution.
Asked in
Google 45 Amazon 38 Meta 25 Microsoft 22
52.0K Views
Medium Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen