MediumArrays · Matrix

Set Matrix Zeroes

Given an m × n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow-up: can you use only O(1) extra space?

[[0,1],[1,0]] → [[0,0],[0,0]][[1,2,3],[4,0,5],[6,7,8]] → [[1,0,3],[0,0,0],[6,0,8]]

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Matrix

Crosshair = the row and column of the cell being checked; new zeros ripple outward

Time and space complexity

m rows, n columns. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Enter any matrix up to 6 × 6 like [[1,2,3],[4,0,5]]; all rows must be the same length.