Minimum Moves to Get a Peaceful Board - Problem
Minimum Moves to Get a Peaceful Board

You are given an n ร— n chessboard with n rooks placed on it. The positions of the rooks are given as a 2D array rooks where rooks[i] = [xi, yi] represents the position of the i-th rook.

Your goal is to rearrange the rooks so that the board becomes peaceful. A board is peaceful when there is exactly one rook in each row and exactly one rook in each column.

You can move rooks one cell at a time, either vertically or horizontally to adjacent cells. However, no two rooks can occupy the same cell at any point during the moves.

Return the minimum number of moves required to make the board peaceful.

Example: If you have 3 rooks on a 3ร—3 board at positions [[0,0], [1,0], [2,2]], you need to move them so each row and column has exactly one rook, like [[0,0], [1,1], [2,2]].

Input & Output

example_1.py โ€” Simple Case
$ Input: rooks = [[0,0],[1,0],[2,2]]
โ€บ Output: 1
๐Ÿ’ก Note: Move the rook from (1,0) to (1,1). Now we have rooks at (0,0), (1,1), (2,2) - each row and column has exactly one rook.
example_2.py โ€” All Same Row
$ Input: rooks = [[0,0],[0,1],[0,2]]
โ€บ Output: 3
๐Ÿ’ก Note: All rooks are in row 0. We need to move them to (0,0), (1,1), (2,2). Total moves: |0-1| + |1-1| + |2-2| + |0-0| + |1-1| + |2-2| = 1 + 0 + 0 + 0 + 0 + 0 = 3 moves (actually 0 + 3 = 3, where 3 comes from row moves)
example_3.py โ€” Already Peaceful
$ Input: rooks = [[0,0],[1,1],[2,2]]
โ€บ Output: 0
๐Ÿ’ก Note: The board is already peaceful - each row and column has exactly one rook, so no moves are needed.

Constraints

  • n == rooks.length
  • 1 โ‰ค n โ‰ค 100
  • rooks[i].length == 2
  • 0 โ‰ค xi, yi โ‰ค n - 1
  • All the values of rooks are unique
  • The input is guaranteed to have exactly n rooks on an n ร— n board

Visualization

Tap to expand
Minimum Moves to Peaceful BoardInitial Board012012Current Positions:Rows: [0, 1, 2]Cols: [0, 0, 2]After Sorting:Rows: [0, 1, 2] โ†’ [0, 1, 2]Cols: [0, 0, 2] โ†’ [0, 0, 2]Target BoardTarget Positions:Rows: [0, 1, 2]Cols: [0, 1, 2]Moves Needed:Row diff: 0+0+0 = 0Col diff: 0+1+0 = 1Total: 1 moveAlgorithm Steps:1. Extract coordinates:Separate row positions [0,1,2] and column positions [0,0,2]2. Sort positions:Current rows [0,1,2] and cols [0,0,2] are matched with target [0,1,2]3. Calculate moves:Sum of |current[i] - target[i]| for rows and columns separatelyTime: O(n log n), Space: O(n)
Understanding the Visualization
1
Identify Current State
List all current rook positions and see which rows/columns have conflicts
2
Define Target
The peaceful arrangement has exactly one rook per row and per column
3
Apply Greedy Matching
Sort current positions and target positions, then match them optimally
Key Takeaway
๐ŸŽฏ Key Insight: Row and column assignments can be optimized independently using greedy matching of sorted positions, leading to minimum total Manhattan distance.
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
38.2K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen