← DiffPush

Set matrix 0's

Standard Bar Arrays · Medium O(m * n) · O(1)

Quarantining Corrupted Rows in a Grid Telemetry Board

A monitoring board stores sensor readings on a rectangular grid. A node reading exactly zero means the bus is faulty, and any row or column containing such a reading must be wiped to zeros to avoid propagating bad data. Perform the wipe in place with constant extra space, without cascading wipes caused by your own edits.

Input: An m x n matrix of integers.

Output: The same matrix with every element of any row or column that originally contained a zero set to 0.

Constraints

Examples

Example 1

Input: {"matrix":[[1,1,1],[1,0,1],[1,1,1]]}
Output: [[1,0,1],[0,0,0],[1,0,1]]
The faulty node lies in the middle, so its entire row and column become zeros.

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]]
Two faulty readings wipe rows 0 and the first and last columns, leaving only the interior cells intact.

Solve this in your browser →

Also on LeetCode ↗