Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
transform-to-chessboard.py 933 Bytes
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-13 01:56 +08:00 . add complexity
# 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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助