Advertisement

Set Matrix Zeroes - LeetCode 73 Solution

Set Matrix Zeroes - Complete Solution Guide

Set Matrix Zeroes is LeetCode problem 73, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an m x n integer matrix matrix , if an element is 0 , set its entire row and column to 0 's. You must do it in place . Example 1: Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] Output: [[1,0,1],[0,0,0],[1,0,1]] 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]] Constraints: m == matrix.length n == matrix[0].length 1 <= m, n <= 200 -2 31 <= matrix[i][j] <= 2 31 - 1 Follow up: A straightforward solution using O(mn) space is probably a bad idea. A si

Detailed Explanation

The problem requires modifying a given m x n integer matrix in-place. If any element in the matrix is 0, we need to set its entire row and column to 0. The challenge lies in achieving this with constant space complexity, as using extra space proportional to the size of the matrix or even the sum of rows and columns is discouraged in the problem description.

Solution Approach

The provided solution uses the first row and first column of the matrix to keep track of the rows and columns that need to be set to zero. It first checks if the first row and first column themselves need to be zeroed out. Then, it iterates through the rest of the matrix. If it finds a zero element, it sets the corresponding element in the first row and first column to zero. After this first pass, it iterates through the matrix again (excluding the first row and column) and sets elements to zero based on the values in the first row and column. Finally, it sets the first row and first column to zero if necessary, based on the initial flags.

Step-by-Step Algorithm

  1. Step 1: Determine if the first row and first column contain any zeros. Store these flags in `first_row_has_zero` and `first_col_has_zero`.
  2. Step 2: Iterate through the matrix, starting from index (1, 1). If `matrix[i][j] == 0`, set `matrix[0][j] = 0` and `matrix[i][0] = 0`. This uses the first row and column as markers.
  3. Step 3: Iterate through the matrix again, starting from index (1, 1). If `matrix[0][j] == 0` or `matrix[i][0] == 0`, set `matrix[i][j] = 0`. This zeroes out the appropriate elements based on the markers.
  4. Step 4: If `first_row_has_zero` is true, set all elements in the first row to 0.
  5. Step 5: If `first_col_has_zero` is true, set all elements in the first column to 0.

Key Insights

  • Insight 1: The main challenge is achieving O(1) space complexity. We cannot use auxiliary arrays of size m or n to store rows and columns to be zeroed out.
  • Insight 2: The first row and first column can be used as flags to indicate which rows and columns need to be zeroed out.
  • Insight 3: We need to handle the first row and first column separately because they are being used as flags. Otherwise, we might incorrectly zero out other rows and columns.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, Matrix.

Companies

Asked at: Adobe, Amazon, Apple, Autodesk, Bloomberg, Expedia, Goldman Sachs, J.P. Morgan, Juspay, Meta, Microsoft, Nutanix, Nvidia, Oracle, ServiceNow, Sprinklr, Uber, Yahoo, eBay, tcs.