MediumNo. 1072
Flip Columns For Maximum Number of Equal Rows — LeetCode 1072 Python Solution
- Pattern
- Matrix and Grid
- Topics
- Array, Hash Table, Matrix
- 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
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
- Time
- O(n)
- Space · auxiliary
- O(n)
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
| No. | Problem | Difficulty |
|---|---|---|
| 1074 | Number of Submatrices That Sum to Target | Hard |
| 0036 | Valid Sudoku | Medium |
| 0037 | Sudoku Solver | Hard |
| 0073 | Set Matrix Zeroes | Medium |
| 0840 | Magic Squares In Grid | Medium |
| 0959 | Regions Cut By Slashes | Medium |
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.