Skip to content
AI360Xpert
Beta
Difficulty: MediumMath & Geometry

Set Matrix Zeroes

Problem in Plain English

Whenever a cell of an m x n matrix is zero, set its whole row and column to zero in place while using only constant extra space.

Problem Statement

Given an m x n integer matrix, if any cell holds 0, set its entire row and column to 0. The update must happen in place.

A copy of the matrix makes the logic trivial but costs m x n extra space, and two marker arrays cost m + n. Aim for a constant-space solution that still reads only the original zero positions.

Constraints

  • m == matrix.length
  • n == matrix[0].length
  • 1≤m,n≤2001 \le m,n \le 200
  • −231≤matrix[i][j]≤231−1-2^{31} \le \text{matrix[i][j]} \le 2^{31}-1

Examples

Example 1

Input
matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
Output
[[1, 0, 1], [0, 0, 0], [1, 0, 1]]

Only (1, 1) originally contains zero. Every cell in row 1 or column 1 becomes zero, while the four corners stay 1. Zeros created during the update do not trigger further changes.

Example 2

Input
matrix = [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]]
Output
[[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]

The original zeros at (0, 0) and (0, 3) clear row 0 and columns 0 and 3. Columns 1 and 2 in the lower rows remain unchanged, even though the finished first row contains zeros there.

Example 3

Input
matrix = [[5, 0, -7]]
Output
[[0, 0, 0]]

The single original zero clears the only row, so every entry becomes zero. There are no interior rows; the first-row flag handles the whole update.

Example 4

Input
matrix = [[-1, 2], [3, 4]]
Output
[[-1, 2], [3, 4]]

No cell originally contains zero. Negative values are ordinary nonzero entries, so the entire matrix stays unchanged.

Intuition

Only zeros present in the original matrix may trigger changes. In Example 1, the original zero at (1, 1) clears row 1 and column 1; the newly created zero at (1, 0) must not clear column 0. Separate discovery from painting: a copy preserves the original evidence, while the optimal approach stores row and column decisions in the matrix borders and remembers the original border decisions in two flags.

Approaches

Copy Matrix

Solution Details

Reveal Copy Matrix: intuition, complexity, and code

Hints

Hint 1
Zeroing eagerly backfires: after you paint row 1, its new zeros look original and column 0 gets painted too.
Hint 2
Decide everything from a frozen snapshot first: a copy, or two marker arrays recording exactly which rows and columns held an original zero.
Hint 3
The first row and first column can hold those markers themselves; keep two flags for whether each of them started with a zero because the corner cell serves both.

Edge Cases

  • With no original zeros, every value including negative numbers remains unchanged.
  • A zero only in the first row, such as [[1,0],[2,3]], clears that row and column 1 but does not clear column 0: the two border flags must remain independent.
  • A zero only in the first column behaves symmetrically; a zero at (0,0) requires both border flags.
  • For a single row or single column, interior scans are empty and the original-border flags decide the result. A zero anywhere clears the sole row or column.
  • An all-zero 200 by 200 matrix remains zero. The copy approach still repaints each original zero's row and column, exposing its larger worst-case time.

Common Mistakes and Interview Tips

  • Painting zeros during the discovery scan, so freshly painted zeros trigger extra rows and columns (the cascading-zero bug).
  • Reusing matrix[0][0] as both border markers without flags, losing whether the first row or the first column was originally zero.
  • Skipping the final border pass, which leaves the first row or column unpainted when the only zero sat inside it.
  • Clearing the first row or column before applying all interior markers destroys which other rows or columns originally needed clearing.
  • Shallow-copying only the outer matrix shares its row arrays with the input, so the copy approach would discover newly created zeros.
  • Returning a newly constructed matrix instead of mutating the supplied one violates the void/None/undefined contract. These snippets preserve every original row object too.
  • Interview tip: explain the original-zero invariant and why the border flags must be captured before marking; then account for repeated painting in the copy approach's time bound.

Key Takeaway

Decisions that must rest on the original state need a snapshot: a copy, marker arrays, or — when space is tight — the matrix borders plus a few flags.