MediumNo. 1072

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

Topics
Array, Hash Table, Matrix
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

PythonSolution
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

Related problems
No.ProblemDifficulty
1074Number of Submatrices That Sum to TargetHard
0036Valid SudokuMedium
0037Sudoku SolverHard
0073Set Matrix ZeroesMedium
0840Magic Squares In GridMedium
0959Regions Cut By SlashesMedium

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.

Free to download. Check it runs on your machine, then pick a plan.