Leetcode 891 Solution

This article provides solution to leetcode question 891 (score-after-flipping-matrix)

https://leetcode.com/problems/score-after-flipping-matrix

Solution

class Solution: def matrixScore(self, A: List[List[int]]) -> int: m = len(A) n = len(A[0])
for i in range(m): if A[i][0] == 1: continue A[i][0] = 1 for j in range(1, n): A[i][j] ^= 1
ans = 0 cur = 1 for j in range(n): one_cnt = 0 for i in range(m): if A[i][n - 1 - j] == 1: one_cnt += 1 ans += cur * max(one_cnt, m - one_cnt) cur *= 2
return ans