代码拉取完成,页面将自动刷新
# Time: O(n^2)
# Space: O(n^2), used by Counter, this could be reduced to O(n) by skipping invalid input
import collections
import itertools
class Solution(object):
def movesToChessboard(self, board):
"""
:type board: List[List[int]]
:rtype: int
"""
N = len(board)
result = 0
for count in (collections.Counter(map(tuple, board)), \
collections.Counter(itertools.izip(*board))):
if len(count) != 2 or \
sorted(count.values()) != [N/2, (N+1)/2]:
return -1
seq1, seq2 = count
if any(x == y for x, y in itertools.izip(seq1, seq2)):
return -1
begins = [int(seq1.count(1) * 2 > N)] if N%2 else [0, 1]
result += min(sum(int(i%2 != v) for i, v in enumerate(seq1, begin)) \
for begin in begins) / 2
return result
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。