Flip Columns For Maximum Number of Equal Rows LeetCode solution in Python

1
from typing import List
2
from collections import Counter
3

4

5
class Solution:
6
def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
7
counter = Counter()
8
max_val = 2 ** len(matrix[0]) - 1
9
for row in matrix:
10
v = self.calculate_binary(row)
11
counter[v] += 1
12
counter[max_val - v] += 1
13
# print(f'counter: {counter}')
14

15
return counter.most_common(1)[0][1]
16

17
def calculate_binary(self, row: List[int]) -> int:
18
val = 0
19
for r in row:
20
val = val * 2 + r
21
return val

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0