n × n board holds n rooks: rooks[i] = [row, col] is the square of rook i (0-indexed). In one move any rook can step to a side-adjacent square. Two rooks may never share a square.
The board is peaceful when every row and every column contains exactly one rook. Return the minimum number of moves to make the board peaceful.rooks=[[0, 1], [0, 2], [2, 2]] → 3
Move the rook from (0, 2) down to (1, 2), then the rook from (2, 2) left to (2, 1) and again to (2, 0). Rows 0, 1, 2 and columns 1, 2, 0 are each used once: 3 moves.
rooks=[[1, 0], [0, 1]] → 0
The board is already peaceful.
rooks=[[2, 2], [2, 1], [2, 0]] → 3
All rooks are in row 2: lift one by one row and another by two rows, 3 moves.
rooks=[[0, 1], [0, 2], [2, 2]]
3
Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.