#73

Set Matrix Zeroes

Medium First row/column as markers

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

If a cell is 0, set its whole row and column to 0. Do it in place, ideally with O(1) extra space.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: any 0 wipes its whole row and column.

Why this works: mark rows and columns first, then wipe; wiping while scanning would create fake zeros. The first row and column can store the marks.

Example: [[1,1,1],[1,0,1],[1,1,1]]

zero at (1,1) → mark row 1, col 1→ [[1,0,1],[0,0,0],[1,0,1]]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Zeroing cells while still scanning — new zeros then wipe out rows and columns they shouldn't.

Solve on LeetCode ↗
Complexity
  • Time: O(m·n).
  • Space: O(1).
Topic: 2D Arrays & Matrix
🎬 Episode 14: video coming soon