Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def movesToChessboard(self, board: List[List[int]]) -> int:
- W = len(board)
- H = len(board[0])
- rows = board
- cols = [[board[x][y] for x in range(W)] for y in range(H)]
- def _check_valid(lines: List[List[int]]) -> bool:
- c = Counter(line[0] for line in lines)
- if abs(c[0] - c[1]) > 1:
- return False
- for i in range(1, len(lines)):
- delta = 1 if lines[0][0] != lines[i][0] else 0
- for j in range(1, len(lines[0])):
- if (delta + lines[0][j]) % 2 != lines[i][j]:
- return False
- return True
- if not _check_valid(rows) or not _check_valid(cols):
- return -1
- def _min_swaps(lines: List[List[int]]) -> int:
- cnt = 0
- expected = 0
- for c in lines[0]:
- if c != expected:
- cnt += 1
- expected = (expected + 1) % 2
- ans_cands = [cnt, len(lines[0]) - cnt]
- ans_cands = [ans for ans in ans_cands if ans % 2 == 0]
- assert ans_cands, "Should have at least one candidate"
- return min(ans_cands) // 2
- return _min_swaps(rows) + _min_swaps(cols)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement