↓Skip to main content
  1. LeetCode/

LeetCode 73: Set Matrix Zeroes

·2 mins· ·
LeetCode Medium Array Hash-Table Matrix
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-25

  • Submissions: Wrong Answer → Accepted → Runtime Error ×2 → Accepted ×2

Approach
#

Do not modify while scanning: a freshly written 0 later looks like an original 0 and spreads across rows and columns. Scan once to record the rows and columns containing 0, then zero them in a second pass, with O(m + n) space. The O(1) version stores the markers in the first row and column, plus one variable for whether the first column itself must be cleared (Matrix Template 2).

My Attempts
#

Attempt 1 zeroed the whole row as soon as it saw a 0, so the rest of that row then read as 0 and their columns were marked too: [[3, 1], [0, 2]] became all zeros instead of [[0, 1], [0, 0]]. Clearing a row only after scanning it passed.

Solution
#

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        m, n = len(matrix), len(matrix[0])
        rows = {i for i in range(m) for j in range(n) if matrix[i][j] == 0}
        cols = {j for i in range(m) for j in range(n) if matrix[i][j] == 0}
        for i in range(m):
            for j in range(n):
                if i in rows or j in cols:
                    matrix[i][j] = 0

Complexity
#

Time O(mn), space O(m + n).

Mistakes to Avoid
#

  • Record everything before modifying; zeroing during the scan makes new zeros look original.

Interview Explanation
#

One pass records the rows and columns containing a 0 and a second pass zeroes them, so new zeros are never mistaken for originals. Space O(m + n); for O(1), keep the markers in the first row and column.

Related

LeetCode 36: Valid Sudoku
·2 mins
LeetCode Medium Array Hash-Table Matrix
LeetCode 36 note: each digit may appear once per row, column and 3 × 3 box; put (digit, row), (digit, column) and (digit, box) keys into one set
LeetCode 74: Search a 2D Matrix
·2 mins
LeetCode Medium Array Binary-Search Matrix
LeetCode 74 note: each row continues the previous one in sorted order, so binary search it as a 1-D array of length m × n, mapping mid to (mid // n, mid % n)
LeetCode 49: Group Anagrams
·2 mins
LeetCode Medium Array Hash-Table String Sorting
LeetCode 49 note: use each string’s sorted form as a key; strings sharing a key form one anagram group
LeetCode 54: Spiral Matrix
·2 mins
LeetCode Medium Array Matrix Simulation
LeetCode 54 note: keep four boundaries and walk right, down, left and up, shrinking a boundary after each side
LeetCode 59: Spiral Matrix II
·2 mins
LeetCode Medium Array Matrix Simulation
LeetCode 59 note: keep four boundaries as in LC 54, but fill in 1 to n² instead of reading
LeetCode 47: Permutations II
·2 mins
LeetCode Medium Array Backtracking Sorting
LeetCode 47 note: sort first, and within a level skip a value equal to the previous one when the previous one is unused