Score After Flipping Matrix

class Solution:
def matrixScore(self, grid: List[List[int]]) -> int:
col_size=len(grid[0])
row_size=len(grid)
mask=(1<<col_size)-1
for i in range(row_size):
val=0
for j in grid[i]:
val<<=1
val|=j
if(val^mask>val):
val1=val^mask
cnt=1
while(val1):
bit = val1&1
grid[i][col_size-cnt]=bit
cnt+=1
val1>>=1
for j in range(col_size):
count_zero=0
count_one=0
for k in range(row_size):
if(grid[k][j]==0):
count_zero+=1
else:
count_one+=1
if(count_zero>count_one):
print(count_zero,"j",j)
for i in range(row_size):
if (grid[i][j]==0):
grid[i][j]=1
else:
grid[i][j]=0
sum_=0
for i in grid:
cnt=1
for j in i:
sum_+=(2**(col_size-cnt))*j
cnt+=1
return sum_

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store