Tap here to switch tabs
Problem
Submissions

Problem: Minimum Swaps to Arrange a Binary Grid

med
30 min
Try to solve the Minimum Swaps to Arrange a Binary Grid problem.

Statement

You are given an n $ imes$ n binary matrix grid. In a single operation, you can choose any two adjacent rows and swap them.

The grid is considered valid if all cells situated strictly above the main diagonal are 00.

Return the minimum number of operations needed to make grid valid, or −1-1 if it is impossible to make it valid.

Note: The main diagonal consists of the cells grid[i][j] where i == j. Cells strictly above the main diagonal are those where i << j.

Constraints:

  • n == grid.length == grid[i].length

  • 1≤1 \leq n ≤200\leq 200

  • grid[i][j] is either 00 or 11

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Swaps to Arrange a Binary Grid

med
30 min
Try to solve the Minimum Swaps to Arrange a Binary Grid problem.

Statement

You are given an n $ imes$ n binary matrix grid. In a single operation, you can choose any two adjacent rows and swap them.

The grid is considered valid if all cells situated strictly above the main diagonal are 00.

Return the minimum number of operations needed to make grid valid, or −1-1 if it is impossible to make it valid.

Note: The main diagonal consists of the cells grid[i][j] where i == j. Cells strictly above the main diagonal are those where i << j.

Constraints:

  • n == grid.length == grid[i].length

  • 1≤1 \leq n ≤200\leq 200

  • grid[i][j] is either 00 or 11