Re: [闲聊] 每日leetcode

楼主: dont   2024-11-22 20:21:53
1072. Flip Columns For Maximum Number of Equal Rows
## 思路
如果有两个row 翻转某些column后变成1111或0000
那两个row的值会完全相同/相反
e.g.
[1,0,0]
[0,1,1]
[1,0,0]
[1,0,0]
对每行的值 以col[0]的值当作标准, 转成mask
用HashTable纪录mask次数, 取最大值
## Code
```python
class Solution:
def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
len_r, len_c = len(matrix), len(matrix[0])
counter = Counter()
for r in range(len_r):
mask = 0
for c in range(len_c):
mask = (mask << 1) | (matrix[r][c] == matrix[r][0])
counter[mask] += 1
return max(counter.values())
```

Links booklink

Contact Us: admin [ a t ] ucptt.com