Skip to main content
Back to problems
#2128
Medium Algorithms

Remove all ones with row and column flips

Array Math Bit Manipulation Matrix
76.2% acceptance
Mar 31, 2026
482
189

No description available.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn remove_ones(grid: Vec<Vec<i32>>) -> bool {
    // Key insight: all rows must be either equal to or the complement of the first row.
    let first = &grid[0];
    for row in &grid {
      let same = row.iter().zip(first).all(|(a, b)| a == b);
      let comp = row.iter().zip(first).all(|(a, b)| a + b == 1);
      if !same && !comp {
        return false;
      }
    }
    true
  }
}