Blind 75 · Math & Geometry

Set Matrix Zeroes

Medium

Watch the walkthrough (5:19)

Problem

Given an m x n matrix, wherever a value is 0, set that value's entire row and entire column to 0.
Do it in place: change the matrix you are given, and return nothing.

Examples

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

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]]

Example 3:
Input: matrix = [[1,2],[3,4]]
Output: [[1,2],[3,4]]

Example 4:
Input: matrix = [[0]]
Output: [[0]]

Constraints

1 <= m, n <= 20
-100 <= matrix[i][j] <= 100

Prerequisites

Index arithmetic on a grid, and the discipline of writing the coordinate rule down before coding it.

How to think about it

1. Marks in the First Row and Column Optimal

Intuition

Every decision has to come from the ORIGINAL grid: zero a row and a column as the scan finds a 0, and the zeros just written are found later and spread again. So mark first and fill after. The marks need a place, and the grid has one: a 0 at (r, c) inside writes a 0 at the start of its row, (r, 0), and at the top of its column, (0, c). But the first row and the first column hold values of their own, and a mark written there would hide whether they held a 0 to begin with. So two flags are noted before any mark is written: does the first row hold a 0, does the first column. Then the marks, from the inner cells; then every inner cell whose row or column is marked is set to 0; and last, the first row and the first column, from the flags. The order is the point: filled first, a zeroed first row reads as a mark in every column. Mark the rows and columns that hold zeros, and after that, fill them with zeros.

Algorithm

1. Note two flags: whether the first row holds a 0, and whether the first column does.
2. Mark: for every inner cell (r and c from 1) that is 0, set matrix[r][0] and matrix[0][c] to 0.
3. Fill: for every inner cell, set it to 0 when matrix[r][0] or matrix[0][c] is 0.
4. Last, if the first row's flag is set, zero the first row; if the first column's is, zero the first column. Return nothing.

Time & Space

Time O(m x n): the grid is scanned a fixed number of times. Space O(1): two flags.

2. Record First, Rewrite Second

Intuition

The trap is cascading: zero a row as you find its 0 and the zeros you just wrote look like zeros of the grid's own, and they spread again. Two passes fix it: decide everything from the ORIGINAL grid, then write. The first pass records which rows and which columns hold a 0; the second sets every cell in a recorded row or a recorded column to 0, in the matrix it was handed. The records cost O(m + n), which the first approach does without by keeping them in the matrix's own first row and column.

Algorithm

1. Record which rows and which columns contain a 0.
2. Set every cell whose row or column was recorded to 0, in matrix. Return nothing.

Time & Space

Time O(m x n). Space O(m + n): the records of the rows and the columns.