LeetCode 73: Set Matrix Zeroes is a Medium matrix problem. This Python walkthrough develops a first-row marker compression solution, ties every code decision to a concrete invariant, and includes the regression check used before publication.
This independent guide paraphrases the task rather than reproducing LeetCode’s prompt. Use the official page for the exact statement, examples, constraints, and submission runner.
| Difficulty | Medium |
|---|---|
| Topic | Matrix |
| Reusable pattern | first-row marker compression |
| Complexity | O(mn) time and O(1) extra space |
Recognizing the pattern
Matrix problems become manageable when row, column, layer, or neighbor boundaries are explicit rather than inferred inside nested loops.
For this problem specifically, use the first row and column as marker storage, preserving separate flags for whether those marker lines originally contained zero. The invariant worth writing beside the code is: A marker at row r or column c records that every cell on that line must become zero.
Step-by-step algorithm
- Identify the input state consumed by
setZeroes(matrix)and initialize the data required by the first-row marker compression pattern. - Use the first row and column as marker storage, preserving separate flags for whether those marker lines originally contained zero.
- After each update, verify the page’s central invariant: A marker at row r or column c records that every cell on that line must become zero.
- Finish only after the boundary behavior is covered: The top-left cell is shared by both marker lines, so row and column flags must be separate.
Python solution
class Solution:
def setZeroes(self, matrix):
rows, cols = len(matrix), len(matrix[0])
first_row = any(matrix[0][c] == 0 for c in range(cols))
first_col = any(matrix[r][0] == 0 for r in range(rows))
for r in range(1, rows):
for c in range(1, cols):
if matrix[r][c] == 0: matrix[r][0] = matrix[0][c] = 0
for r in range(1, rows):
for c in range(1, cols):
if matrix[r][0] == 0 or matrix[0][c] == 0: matrix[r][c] = 0
if first_row:
for c in range(cols): matrix[0][c] = 0
if first_col:
for r in range(rows): matrix[r][0] = 0Reading the implementation
The main entry point is setZeroes(matrix). The named working state includes rows, first_row, first_col; those variables make the first-row marker compression state visible instead of hiding it in incidental control flow.
The implementation uses 6 loops with separate responsibilities, so preprocessing and the main traversal can each stay linear in their own input. In concrete terms, use the first row and column as marker storage, preserving separate flags for whether those marker lines originally contained zero.
Correctness argument
Initialization. The data structure starts with exactly the information known before any input element is processed.
Preservation. Use the first row and column as marker storage, preserving separate flags for whether those marker lines originally contained zero. Each update records the current item without invalidating earlier decisions; consequently, a marker at row r or column c records that every cell on that line must become zero.
Termination. The traversal consumes a finite input or finite state space. At the end, the invariant covers the complete input, which is precisely the condition required for the returned result.
Complexity and trade-offs
O(mn) time and O(1) extra space. The auxiliary-space figure excludes the returned output unless the output itself is the structure being built.
An auxiliary matrix can make reads and writes independent, while marker or boundary techniques trade that clarity for lower space use. That comparison is useful in an interview because it explains why the final implementation is preferable, not merely that it passes.
Regression check
The published implementation belongs to a 100-problem suite that is compiled and exercised behaviorally before deployment.
One reference assertion from that suite is shown below. It targets the normal path while the edge conditions in the next section cover the failure-prone boundaries.
matrix=[[1,1,1],[1,0,1],[1,1,1]]; Solution().setZeroes(matrix); assert matrix == [[1,0,1],[0,0,0],[1,0,1]]Common mistakes and edge cases
- Problem-specific boundary: The top-left cell is shared by both marker lines, so row and column flags must be separate.
- Pattern-level pitfall: Rectangular inputs, one-row layers, and one-column layers expose off-by-one errors that square examples often hide.
- Invariant check: after every update, confirm that a marker at row r or column c records that every cell on that line must become zero.
Interview review checklist
- Explain why first-row marker compression matches the structure of this input.
- State the invariant in one sentence before tracing code: A marker at row r or column c records that every cell on that line must become zero.
- Derive O(mn) time and O(1) extra space from how many times each element or state is visited.
- Test the boundary explicitly: The top-left cell is shared by both marker lines, so row and column flags must be separate.
Browse the searchable 100 LeetCode Python Solutions hub. Previous: 48. Rotate Image · Next: 289. Game of Life