Skip to content
BytePatterns

Zero Out Rows And Columns

MediumMatrix & Grid#grid-marking#in-place~25m

Problem

Given a grid of whole numbers, every cell holding a zero must clear its entire row and its entire column. Rewrite the grid in place, using the values as they were before any clearing began.

Examples

Input:  grid = [[1, 1, 1],
                [1, 0, 1],
                [1, 1, 1]]
Output: [[1, 0, 1],
         [0, 0, 0],
         [1, 0, 1]]
Input:  grid = [[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]]
Input:  grid = [[1, 2],
                [3, 4]]
Output: [[1, 2],
         [3, 4]]
Why:    edge case, no zeros means nothing changes

Hints

0 / 3

Stuck on the idea rather than the code? Rotate In Place covers it.