-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0861-score-after-flipping-matrix.py
More file actions
35 lines (27 loc) · 894 Bytes
/
0861-score-after-flipping-matrix.py
File metadata and controls
35 lines (27 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# time complexity: O(m*n)
# space complexity: O(m*n)
from typing import List
class Solution:
def matrixScore(self, grid: List[List[int]]) -> int:
row = len(grid)
col = len(grid[0])
for i in range(row):
if grid[i][0] == 0:
for j in range(col):
grid[i][j] ^= 1
for j in range(1, col):
countZero = 0
for i in range(row):
if grid[i][j] == 0:
countZero += 1
if countZero > row - countZero:
for i in range(row):
grid[i][j] ^= 1
score = 0
for i in range(row):
for j in range(col):
colScore = grid[i][j] << (col-j-1)
score += colScore
return score
grid = [[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]]
print(Solution().matrixScore(grid))