forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScore After Flipping Matrix.py
44 lines (31 loc) · 1.18 KB
/
Score After Flipping Matrix.py
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
36
37
38
39
40
41
42
43
44
class Solution:
def calcScore(self):
score = 0
for row in self.grid:
for i in range(self.width):
score += row[i] * (2 ** (self.width - i - 1))
return score
def flipRow(self, row):
for i in range(self.width):
self.grid[row][i] = int(not self.grid[row][i])
def flipCol(self, col):
for i in range(self.height):
self.grid[i][col] = int(not self.grid[i][col])
def colSum(self, col):
total = 0
for i in range(self.height):
total += self.grid[i][col]
return total
def matrixScore(self, grid: List[List[int]]) -> int:
self.grid = grid
self.height = len(grid)
self.width = len(grid[0])
for r in range(self.height):
if not self.grid[r][0]:
if not self.grid[r][0]:
self.flipRow(r)
for c in range(1, self.width):
colSum = self.colSum(c)
if colSum < ceil(self.height/2):
self.flipCol(c)
return self.calcScore()