798. Transform to Chessboard
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of row and column swaps to transform a grid into a chessboard. A chessboard has no two adjacent cells (up, down, left, right) with the same value. This means values must alternate in every row and column, like 0101... and 1010....
Solution
Transform to Chessboard in Python
class Solution:
def movesToChessboard(self, board: list[list[int]]) -> int:
n = len(board)
# Check if the board has the chessboard property.
# For any i, j, board[i][j] ^ board[0][0] ^ board[i][0] ^ board[0][j] must be 0.
for i in range(n):
for j in range(n):
if (board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]) == 1:
return -1
# Count ones in the first row and first column.
row_sum = sum(board[0])
col_sum = sum(board[i][0] for i in range(n))
# Check if the counts of ones are valid.
if n % 2 == 0: # n is even
if row_sum != n // 2 or col_sum != n // 2:
return -1
else: # n is odd
if not (n // 2 <= row_sum <= n // 2 + 1):
return -1
if not (n // 2 <= col_sum <= n // 2 + 1):
return -1
# Calculate misplaced elements for rows and columns against the 0101... pattern.
row_misplaced = 0
col_misplaced = 0
for i in range(n):
if board[i][0] != i % 2:
row_misplaced += 1
if board[0][i] != i % 2:
col_misplaced += 1
row_swaps = 0
col_swaps = 0
if n % 2 == 0: # n is even, choose the target pattern with fewer swaps.
row_swaps = min(row_misplaced, n - row_misplaced) // 2
col_swaps = min(col_misplaced, n - col_misplaced) // 2
else: # n is odd, the target pattern is fixed.
# If misplaced count is odd, we must be targeting the opposite pattern.
if row_misplaced % 2 != 0:
row_swaps = (n - row_misplaced) // 2
else:
row_swaps = row_misplaced // 2
if col_misplaced % 2 != 0:
col_swaps = (n - col_misplaced) // 2
else:
col_swaps = col_misplaced // 2
return row_swaps + col_swaps
Transform to Chessboard in Java
class Solution {
public int movesToChessboard(int[][] board) {
int n = board.length;
// Check if the board has the chessboard property.
// For any i, j, board[i][j] ^ board[0][0] ^ board[i][0] ^ board[0][j] must be 0.
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if ((board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]) == 1) {
return -1;
}
}
}
// Count ones in the first row and first column.
int rowSum = 0, colSum = 0;
for (int i = 0; i < n; ++i) {
rowSum += board[0][i];
colSum += board[i][0];
}
// Check if the counts of ones are valid.
if (n % 2 == 0) { // n is even
if (rowSum != n / 2 || colSum != n / 2) {
return -1;
}
} else { // n is odd
if (rowSum < n / 2 || rowSum > n / 2 + 1) {
return -1;
}
if (colSum < n / 2 || colSum > n / 2 + 1) {
return -1;
}
}
// Calculate misplaced elements for rows and columns against the 0101... pattern.
int rowMisplaced = 0, colMisplaced = 0;
for (int i = 0; i < n; ++i) {
if (board[i][0] != i % 2) {
rowMisplaced++;
}
if (board[0][i] != i % 2) {
colMisplaced++;
}
}
int rowSwaps = 0, colSwaps = 0;
if (n % 2 == 0) { // n is even, choose the target pattern with fewer swaps.
rowSwaps = Math.min(rowMisplaced, n - rowMisplaced) / 2;
colSwaps = Math.min(colMisplaced, n - colMisplaced) / 2;
} else { // n is odd, the target pattern is fixed.
// If misplaced count is odd, we must be targeting the opposite pattern.
if (rowMisplaced % 2 != 0) {
rowSwaps = (n - rowMisplaced) / 2;
} else {
rowSwaps = rowMisplaced / 2;
}
if (colMisplaced % 2 != 0) {
colSwaps = (n - colMisplaced) / 2;
} else {
colSwaps = colMisplaced / 2;
}
}
return rowSwaps + colSwaps;
}
}
Transform to Chessboard in C++
class Solution {
public:
int movesToChessboard(vector<vector<int>>& board) {
int n = board.size();
// Check if the board has the chessboard property.
// For any i, j, board[i][j] ^ board[0][0] ^ board[i][0] ^ board[0][j] must be 0.
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if ((board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]) == 1) {
return -1;
}
}
}
// Count ones in the first row and first column.
int rowSum = 0, colSum = 0;
for (int i = 0; i < n; ++i) {
rowSum += board[0][i];
colSum += board[i][0];
}
// Check if the counts of ones are valid.
if (n % 2 == 0) { // n is even
if (rowSum != n / 2 || colSum != n / 2) {
return -1;
}
} else { // n is odd
if (rowSum < n / 2 || rowSum > n / 2 + 1) {
return -1;
}
if (colSum < n / 2 || colSum > n / 2 + 1) {
return -1;
}
}
// Calculate misplaced elements for rows and columns against the 0101... pattern.
int rowMisplaced = 0, colMisplaced = 0;
for (int i = 0; i < n; ++i) {
if (board[i][0] != i % 2) {
rowMisplaced++;
}
if (board[0][i] != i % 2) {
colMisplaced++;
}
}
int rowSwaps = 0, colSwaps = 0;
if (n % 2 == 0) { // n is even, choose the target pattern with fewer swaps.
rowSwaps = min(rowMisplaced, n - rowMisplaced) / 2;
colSwaps = min(colMisplaced, n - colMisplaced) / 2;
} else { // n is odd, the target pattern is fixed.
// If misplaced count is odd, we must be targeting the opposite pattern.
if (rowMisplaced % 2 != 0) {
rowSwaps = (n - rowMisplaced) / 2;
} else {
rowSwaps = rowMisplaced / 2;
}
if (colMisplaced % 2 != 0) {
colSwaps = (n - colMisplaced) / 2;
} else {
colSwaps = colMisplaced / 2;
}
}
return rowSwaps + colSwaps;
}
};
Transform to Chessboard in TypeScript
function movesToChessboard(board: number[][]): number {
const n = board.length;
// Check if the board has the chessboard property.
// For any i, j, board[i][j] ^ board[0][0] ^ board[i][0] ^ board[0][j] must be 0.
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
if ((board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]) === 1) {
return -1;
}
}
}
// Count ones in the first row and first column.
let rowSum = 0, colSum = 0;
for (let i = 0; i < n; ++i) {
rowSum += board[0][i];
colSum += board[i][0];
}
// Check if the counts of ones are valid.
if (n % 2 === 0) { // n is even
if (rowSum !== n / 2 || colSum !== n / 2) {
return -1;
}
} else { // n is odd
if (rowSum < Math.floor(n / 2) || rowSum > Math.floor(n / 2) + 1) {
return -1;
}
if (colSum < Math.floor(n / 2) || colSum > Math.floor(n / 2) + 1) {
return -1;
}
}
// Calculate misplaced elements for rows and columns against the 0101... pattern.
let rowMisplaced = 0, colMisplaced = 0;
for (let i = 0; i < n; ++i) {
if (board[i][0] !== i % 2) {
rowMisplaced++;
}
if (board[0][i] !== i % 2) {
colMisplaced++;
}
}
let rowSwaps = 0, colSwaps = 0;
if (n % 2 === 0) { // n is even, choose the target pattern with fewer swaps.
rowSwaps = Math.min(rowMisplaced, n - rowMisplaced) / 2;
colSwaps = Math.min(colMisplaced, n - colMisplaced) / 2;
} else { // n is odd, the target pattern is fixed.
// If misplaced count is odd, we must be targeting the opposite pattern.
if (rowMisplaced % 2 !== 0) {
rowSwaps = (n - rowMisplaced) / 2;
} else {
rowSwaps = rowMisplaced / 2;
}
if (colMisplaced % 2 !== 0) {
colSwaps = (n - colMisplaced) / 2;
} else {
colSwaps = colMisplaced / 2;
}
}
return rowSwaps + colSwaps;
}
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.