Re: [閒聊] 每日leetcode

作者: DJYOSHITAKA (Evans)   2024-05-13 22:02:07
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
作者: digua (地瓜)   2023-05-13 22:02:00
大師
作者: argorok (s.green)   2024-05-13 22:03:00
別捲了
作者: sustainer123 (caster)   2024-05-13 22:08:00
我現在也整天np來np去 唉
作者: RinNoKareshi (立石凜的男友)   2024-05-13 22:17:00
又帥又會寫code

Links booklink

Contact Us: admin [ a t ] ucptt.com