class Solution:
def matrixScore(self, grid: List[List[int]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
for r in range(ROWS):
if grid[r][0] == 0:
for c in range(COLS):
grid[r][c] ^= 1
for c in range(COLS):
one_cnt = sum(grid[r][c] for r in range(ROWS))
if one_cnt < ROWS - one_cnt:
for r in range(ROWS):
grid[r][c] ^= 1
res = 0
for r in range(ROWS):
for c in range(COLS):
res += grid[r][c] << (COLS - c - 1)
return resWhere is the number of rows and is the number of columns.
class Solution:
def matrixScore(self, grid: List[List[int]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
res = ROWS * (1 << (COLS - 1))
for c in range(1, COLS):
cnt = 0
for r in range(ROWS):
if grid[r][c] != grid[r][0]:
cnt += 1
cnt = max(cnt, ROWS - cnt)
res += cnt * (1 << (COLS - c - 1))
return resWhere is the number of rows and is the number of columns.