Trains the technique from
LeetCode 1536Minimum Swaps to Arrange a Binary GridThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A stencil sheet is a square grid of 0s and 1s given as grid, with n rows and n columns.
One shuffle swaps two rows that sit next to each other. The sheet is usable when every cell strictly above the main diagonal holds a 0, that is when grid[i][j] is 0 for every j greater than i.
Return the fewest shuffles that make the sheet usable, or -1 when no number of shuffles can. A shuffle count is never negative, so -1 can only mean it is impossible.
Example 1
The top row already has the two trailing zeros it needs. The second row needs one trailing zero and ends in a 1, so the third row is brought up past it.
Example 2
Both rows end in a 1, so neither can sit on top where a trailing zero is needed, and no shuffle helps.
Example 3
Every cell above the diagonal already holds a 0, so the sheet is usable as it stands.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def row_shuffles(grid: list[list[int]]) -> int:public int rowShuffles(int[][] grid)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.