DSA · Arrays & Matrix · #162
Set Matrix Zeroes
Module 81 · difficulty 3/5·⏱ 30:00starts on first keystroke
Given an `m x n` integer matrix, if an element is `0`, set its entire row and column to `0`. You must do it **in place** and return the same matrix reference. Implement `setZeroes(matrix)`. The function mutates `matrix` directly (and returns it for convenience). Aim for a constant-space solution that does not allocate another full `m x n` matrix.
Examples
matrix = [[1,1,1],[1,0,1],[1,1,1]]→[[1,0,1],[0,0,0],[1,0,1]]— The single 0 at (1,1) zeroes out row 1 and column 1.matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]→[[0,0,0,0],[0,4,5,0],[0,3,1,0]]— Two zeros in row 0 wipe columns 0 and 3 plus the whole of row 0.matrix = [[1,2,3]]→[[1,2,3]]— No zeros means the matrix is unchanged.
Constraints
- · m == matrix.length
- · n == matrix[0].length
- · 1 <= m, n <= 200
- · -2^31 <= matrix[i][j] <= 2^31 - 1
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.