Flip Columns For Maximum Number of Equal Rows — LeetCode 1072 Python Solution
MediumArrayHash TableMatrix
- Problem
- #1072
- Pattern
- Matrix and Grid
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given an m x n binary matrix matrix. You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from 0 to 1 or vice versa).
Example
- Input
- matrix = [[0,1],[1,1]]
- Output
- 1
- Explanation
- After flipping no values, 1 row has all values equal.
Python solution
Python
class Solution:
def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
cnt = Counter()
for row in matrix:
t = tuple(row) if row[0] == 0 else tuple(x ^ 1 for x in row)
cnt[t] += 1
return max(cnt.values())Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) auxiliary |
Pattern: Matrix and Grid
Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 1072. Flip Columns For Maximum Number of Equal Rows is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.
The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1072. Flip Columns For Maximum Number of Equal Rows?
- LeetCode 1072. Flip Columns For Maximum Number of Equal Rows is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1072. Flip Columns For Maximum Number of Equal Rows?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1072. Flip Columns For Maximum Number of Equal Rows?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 1072. Flip Columns For Maximum Number of Equal Rows cover?
- LeetCode 1072. Flip Columns For Maximum Number of Equal Rows is tagged Array, Hash Table and Matrix on LeetCode.