Minimum Swaps to Arrange a Binary Grid - Problem
Minimum Swaps to Arrange a Binary Grid
You're given an
A grid is considered valid when all cells above the main diagonal contain zeros. The main diagonal runs from the top-left corner
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
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.
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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code