Transform to Chessboard - Problem
Transform to Chessboard
You are given an
In each move, you can:
⢠Swap any two rows with each other
⢠Swap any two columns with each other
A chessboard pattern is a board where no two adjacent cells (horizontally or vertically) have the same value - just like a real chessboard where black and white squares alternate.
Return: The minimum number of moves needed to create a chessboard pattern, or
Example:
You are given an
n x n binary grid board filled with 0s and 1s. Your goal is to transform this board into a valid chessboard pattern using the minimum number of moves.In each move, you can:
⢠Swap any two rows with each other
⢠Swap any two columns with each other
A chessboard pattern is a board where no two adjacent cells (horizontally or vertically) have the same value - just like a real chessboard where black and white squares alternate.
Return: The minimum number of moves needed to create a chessboard pattern, or
-1 if it's impossible.Example:
Input: [[0,1,1,0], Output: 2
[0,1,1,0],
[1,0,0,1],
[1,0,0,1]]This can be transformed into a chessboard by swapping 2 rows or 2 columns. Input & Output
example_1.py ā Basic Transform
$
Input:
board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
āŗ
Output:
2
š” Note:
The board has 2 unique row patterns: [0,1,1,0] and [1,0,0,1] which are complements. Same for columns. We can swap row 0 with row 2, and the result will be a valid chessboard pattern. Total moves needed: 2.
example_2.py ā Already Valid
$
Input:
board = [[0,1],[1,0]]
āŗ
Output:
0
š” Note:
This 2x2 board is already a valid chessboard pattern where no adjacent cells have the same value. No moves needed.
example_3.py ā Impossible Case
$
Input:
board = [[1,1,0],[0,0,1],[0,0,1]]
āŗ
Output:
-1
š” Note:
This board cannot be transformed into a chessboard. There are more than 2 unique row patterns, and the patterns are not proper complements of each other.
Visualization
Tap to expand
Understanding the Visualization
1
Pattern Discovery
Identify all unique row and column patterns in the board
2
Validity Check
Verify there are exactly 2 patterns for rows and columns, each being complements
3
Count Analysis
Ensure pattern counts are balanced (differ by at most 1)
4
Optimal Target
Determine which chessboard arrangement requires fewer swaps
5
Move Calculation
Count minimum row swaps and column swaps needed
Key Takeaway
šÆ Key Insight: Only boards with exactly 2 complementary patterns for both rows and columns can be transformed into chessboards. The mathematical approach provides O(n²) optimal solution.
Time & Space Complexity
Time Complexity
O(n²)
Single pass through the nĆn board to analyze patterns
ā Quadratic Growth
Space Complexity
O(n)
Space to store unique row and column patterns
ā” Linearithmic Space
Constraints
- n == board.length
- n == board[i].length
- 2 ⤠n ⤠30
- board[i][j] is either 0 or 1
- Only boards with exactly 2 complementary patterns can be transformed
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code