Set Matrix Zeroes
If a cell is zero, set its entire row and column to zero in place.
Why does this pattern fit?
Restate the exact job
If a cell is zero, set its entire row and column to zero in place.
The first row and column can store markers, with separate flags for their original state.
O(mn) time · O(1) space
Zeroing immediately creates new zeros and spreads too far.
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.