861. Score After Flipping Matrix
就第0個column一定要翻成1,所以先決定每個row要不要翻
接下來後面每個column看1多還0多,0多就翻column,1多就不翻
好久沒有會寫的題目了
哭了
不過我好久沒有寫py了
以前都np來np去 不然就是model.train()
根本不能算是寫py 唉
寫得一坨
我py流淚了
def matrixScore(self, grid: List[List[int]]) -> int:
row_oddeven = []
row_num = len(grid)
col_num = len(grid[0])
ans = (2 ** (col_num-1)) * row_num
for i in range(row_num):
row_oddeven.append(int(grid[i][0] == 0))
for j in range(1, col_num):
col_sum = sum([grid[i][j] ^ row_oddeven[i] for i in range(row_num)])
if col_sum > row_num//2:
ans += (2 ** (col_num-j-1)) * col_sum
else:
ans += (2 ** (col_num-j-1)) * (row_num - col_sum)
return ans