Problem statement

https://binarysearch.com/problems/Flipped-Matrix/

Solution

Equal to Leetcode 0861 Score After Flipping Matrix.

Complexity

It is O(mn) for time and O(1) for space.

Code

class Solution:
    def solve(self, A):
        m, n = len(A), len(A[0])
        ans = (1<<(n-1))*m
        
        for j in range(1, n):
            cand = sum(A[i][0]^A[i][j] for i in range(m))
            ans += max(cand, m - cand)*1<<(n-1-j)
        
        return ans