Brute Force
class Solution(object):
def matrixScore(self, A):
R, C = len(A), len(A[0])
colsums = [0] * C
for r in xrange(R):
for c in xrange(C):
colsums[c] += A[r][c]
ans = 0
for r in xrange(1 left shift R):
if r:
trans = r ^ (r-1)
for bit in xrange(R):
if (trans >> bit) and 1:
for c in xrange(C):
colsums[c] += -1 if A[bit][c] else +1
A[bit][c] ^= 1
score = sum(max(x, R - x) * (1 left shift (C-1-c))
for c, x in enumerate(colsums))
ans = max(ans, score)
return ans
Greedy
class Solution(object):
def matrixScore(self, A):
R, C = len(A), len(A[0])
ans = 0
for c in xrange(C):
col = 0
for r in xrange(R):
col += A[r][c] ^ A[r][0]
ans += max(col, R - col) * 2 ** (C - 1 - c)
return ans
That didn't work. Go here: https://leetcode.com/problems/score-after-flipping-matrix/solution/