-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path861.py
48 lines (41 loc) · 1.49 KB
/
861.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
45
46
47
48
__________________________________________________________________________________________________
sample 36 ms submission
class Solution:
def matrixScore(self, A: List[List[int]]) -> int:
n = len(A)
for i in range(n):
if A[i][0] == 0:
A[i] = [1 - x for x in A[i]]
result = 0
for i in map(sum, zip(*A)):
result = result * 2 + max(i, n - i)
return result
__________________________________________________________________________________________________
sample 12980 kb submission
class Solution:
def matrixScore(self, A: List[List[int]]) -> int:
R, C = len(A), len(A[0])
# ensure 1st item in each row is 1
for i in range(R):
if A[i][0] == 0:
for j in range(C):
A[i][j] = 1 - A[i][j]
for i in range(C):
tmp = 0
for j in range(R):
if A[j][i] == 0:
tmp += 1
if tmp * 2 > R:
for k in range(R):
A[k][i] = 1 - A[k][i]
return self._matrixToScore(A)
def _matrixToScore(self, A):
R, C = len(A), len(A[0])
score = 0
for i in range(R):
tmp = 0
for j in range(C):
tmp = (tmp << 1) | A[i][j]
score += tmp
return score
__________________________________________________________________________________________________