Problem 336996 · medium · Phase 03 Linear Management & Searching

Blank Out Rows and Columns

matrix · in-place · marking

Given an m x n integer grid matrix, every cell whose value is 0 "blanks out" its entire row and its entire column: all of those cells must become 0. Perform the change in place and return matrix.

Be careful: a cell that you set to 0 during the process must not itself blank out further rows or columns. Only the zeros present in the input count.

Examples

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

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]]
Explanation: row 0 and columns 0 and 3 are cleared.

Constraints

  • 1 <= m, n <= 200
  • Target: O(m * n) time. A first solution may use O(m + n) extra space; the follow-up is O(1).

Goals

  • Record which rows and columns must be cleared before changing anything
  • Use the first row and column of the grid itself as the record to reach O(1) extra space
  • Return the mutated grid
Starting Python…