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).
Return the maximum number of rows that have all values equal after some number of flips.
Example 1:
Input: matrix = [[0,1],[1,1]] Output: 1 Explanation: After flipping no values, 1 row has all values equal.
Example 2:
Input: matrix = [[0,1],[1,0]] Output: 2 Explanation: After flipping values in the first column, both rows have equal values.
Example 3:
Input: matrix = [[0,0,0],[0,0,1],[1,1,0]] Output: 2 Explanation: After flipping values in the first two columns, the last two rows have equal values.
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 300
matrix[i][j]
is either 0
or 1
.The problem asks to find the maximum number of rows in a binary matrix that have all values equal after flipping some columns. Flipping a column means inverting all the bits (0s become 1s and vice versa) in that column. The key insight is that we don't need to explicitly try all possible column flips. Instead, we can represent each row by a string (or tuple) where we normalize the row to start with 0. This means if the first element of a row is 1, we flip the entire row before creating the string representation. This way, rows that are equivalent after any number of flips will have the same string representation.
Normalization: For each row, check the first element. If it's 1, flip all bits in the row (using XOR with 1). This ensures that all equivalent rows (after potential column flips) will have the same representation starting with 0.
Counting: Convert each row into a string (or tuple) representing its normalized bit pattern. Use a hash map (dictionary in Python) to count the occurrences of each unique string.
Maximum Count: The maximum count among all strings represents the maximum number of rows with all values equal after some flips. This is because each unique string represents a set of rows that are equivalent after column flips.
Time Complexity: O(m*n), where 'm' is the number of rows and 'n' is the number of columns. This is because we iterate through each row and each element in each row once. The hash map operations (insertion and lookup) are on average O(1).
Space Complexity: O(m), where 'm' is the number of rows (in the worst case, if all rows are unique). This is because the hash map stores at most 'm' unique string representations.
from collections import Counter
class Solution:
def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
cnt = Counter() # Uses Counter for efficient counting of strings
for row in matrix:
# Normalize the row: If the first element is 1, flip the whole row.
t = tuple(row) if row[0] == 0 else tuple(x ^ 1 for x in row)
cnt[t] += 1 # Increment the count for the normalized row string
return max(cnt.values()) # Return the maximum count
The Python code uses the Counter
object from the collections
module which makes counting the occurrences of tuples very efficient. Other languages use similar hash map (dictionary) structures to achieve the same effect. The core logic remains the same across all languages. The Java, C++, Go, and TypeScript versions simply adapt the data structures to their respective language features.