PROBLEM 70 OF 75

Set Matrix Zeroes

If a cell is zero, set its entire row and column to zero in place.

PATTERNMatrix traversal & transformsWrite the coordinate mapping before the loops.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

If a cell is zero, set its entire row and column to zero in place.

WHY THIS FITS

The first row and column can store markers, with separate flags for their original state.

COMPLEXITY

O(mn) time · O(1) space

COMMON FAILURE

Zeroing immediately creates new zeros and spreads too far.

THE REASONING, IN ONE PLACE

How to solve Set Matrix Zeroes

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

If a cell is zero, set its entire row and column to zero in place.

Why Matrix traversal & transforms fits

The first row and column can store markers, with separate flags for their original state.

State to maintain

First-row/column markers plus two booleans.

Transition

Mark from interior, zero marked rows/columns, then handle the first row and column.

Time and space

O(mn) time · O(1) space

Counterexample to the tempting mistake

Zeroing immediately creates new zeros and spreads too far.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗