Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
For each row of the grid calculate the most right 1 in the grid in the array maxRight.
To check if there exist answer, sort maxRight and check if maxRight[i] ≤ i for all possible i's.
If there exist an answer, simulate the swaps.