#1072
Medium Algorithms Flip columns for maximum number of equal rows
Array Hash Table Matrix
78.6% acceptance
Feb 25, 2026
1344
130
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn max_equal_rows_after_flips(matrix: Vec<Vec<i32>>) -> i32 {
let mut count: std::collections::HashMap<Vec<i32>, i32> = std::collections::HashMap::new();
for row in &matrix {
let key: Vec<i32> = row.iter().map(|&v| v ^ row[0]).collect();
*count.entry(key).or_insert(0) += 1;
}
*count.values().max().unwrap_or(&0)
}
}