Show problems as
Classic problem #73MediumArrayHash TableMatrix

Set Matrix Zeroes

Real-world version: Blank The Failed Sites
Tracked in this browser only
Read Set Matrix Zeroes on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A test bench records one signed offset per site of a panel into sheet, a grid of rows rows by cols columns. A site whose offset came out as exactly 0 failed its probe, and a failed probe throws doubt on every site sharing its row and every site sharing its column.

Write that doubt into the sheet. Once you are done, a site must hold 0 when it failed, when any site in its row failed, or when any site in its column failed. Every other site must still hold the offset it arrived with.

Failures are judged from the offsets as they were handed to you. A 0 you wrote yourself never counts as a failure and must not spread further.

The bench firmware has no room for a second panel of this size, so rewrite sheet in the storage it already occupies. Beyond a fixed handful of scratch values, no extra space may be used, however large rows and cols grow. Return sheet after marking it.

Examples

Example 1

Input
sheet = [[4, 0], [7, 9]]
Output
[[0, 0], [7, 0]]

The top right site failed, so the top row and the right column are blanked. The bottom left site keeps its offset because neither its row nor its column holds a failure.

Example 2

Input
sheet = [[3, 8, 5], [6, 0, 2], [9, 4, 7]]
Output
[[3, 0, 5], [0, 0, 0], [9, 0, 7]]

Only the middle site failed, which blanks the middle row and the middle column and leaves the four corners untouched.

Example 3

Input
sheet = [[0, 6], [5, 5]]
Output
[[0, 0], [0, 5]]

The failure sits in the first row and the first column, so both of them are blanked, and only the bottom right site survives.

Constraints

  • rows == sheet.length
  • cols == sheet[0].length
  • 1 <= rows, cols <= 200
  • -2^31 <= sheet[r][c] <= 2^31 - 1
  • Only a fixed amount of extra space may be used; no second grid of size rows by cols

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def blank_failed_sites(sheet: list[list[int]]) -> list[list[int]]:
Java
public int[][] blankFailedSites(int[][] sheet)