Flip Columns For Maximum Number of Equal Rows — LeetCode 1072 Python Solution

MediumArrayHash TableMatrix
Problem
#1072
Reading time
2 min

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

MeasureComplexity
TimeO(n)
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview