This is a premium problem. We're working on making it available for free soon.
Explore Free ProblemsUse these hints if you're stuck. Try solving on your own first.
Think of a greedy method.
First, distribute the rooks in individual rows.
You can do this by sorting all rooks by their rows. Then assign the first one to the first row, the second one to the second row, and so on.
After you've distributed rooks across all rows, now do the same for columns.
Sort rooks by their columns and then assign the first one to the first column and so on.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, problems like this appear in coding interviews because they test greedy thinking, sorting techniques, and reasoning about coordinate transformations. Variants involving board balancing or minimizing movement are common in technical interviews.
The optimal approach separates row and column adjustments. Extract the row and column indices of all pieces, sort them, and align them with target positions so each row and column has exactly one piece. The minimum moves are calculated using the sum of absolute differences between current and target positions.
Arrays are the main data structure used to store row and column indices. Sorting algorithms or counting sort help efficiently align current positions with their target placements to minimize movement.
Row and column conflicts do not affect each other when computing minimal Manhattan movement. By optimizing row positions and column positions separately, you still achieve the global minimum number of moves needed to make the board peaceful.