Skip to content

Practice Transform to Chessboard

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#782
LeetCode Problem

Transform to Chessboard

Problem Statement

You are given an n x n binary grid board. In each move, you can swap any two rows with each other, or any two columns with each other. Return the minimum number of moves to transform the board into a chessboard board. If the task is impossible, return -1. A chessboard board is a board where no 0's and no 1's are 4-directionally adjacent.

Source: Transform to Chessboard on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
Output
2

# Example 2

Input
board = [[0,1],[1,0]]
Output
0

# Example 3

Input
board = [[1,0],[1,0]]
Output
-1

Constraints

n == board.length
n == board[i].length
2 <= n <= 30
board[i][j] is either 0 or 1.

Related LeetCode Problems

Practice more problems similar to Transform to Chessboard.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.