Leetcode 73: Set Matrix Zeroes
You are given an m x n matrix of integers. Whenever an element in the matrix is 0, you need to set all elements in the corresponding row and column to 0, but the operation must be done in place. This means you cannot use extra space for another matrix.
Problem
Approach
Steps
Complexity
Input: You are given a matrix of size m x n containing integers.
Example: matrix = [[3, 5, 1], [4, 0, 2], [7, 8, 9]]
Constraints:
• 1 <= m, n <= 200
• -231 <= matrix[i][j] <= 231 - 1
Output: Return the transformed matrix where rows and columns containing zeros are set to zero in place.
Example: [[3, 0, 1], [0, 0, 0], [7, 0, 9]]
Constraints:
• The output should be the matrix itself, modified in place.
Goal: Transform the matrix in place by setting entire rows and columns to 0 whenever a zero is encountered.
Steps:
• Identify if the first row or column contains a zero, and mark these as flags.
• Use the first row and column to mark which rows and columns need to be zeroed.
• Iterate through the rest of the matrix and use the flags to set the corresponding rows and columns to zero.
• Handle the first row and column separately if necessary.
Goal: Constraints on the matrix dimensions and values.
Steps:
• 1 <= m, n <= 200
• -231 <= matrix[i][j] <= 231 - 1
Assumptions:
• The matrix is a 2D array with at least one element.
• All elements in the matrix are integers.
• Input: matrix = [[3, 5, 1], [4, 0, 2], [7, 8, 9]]
• Explanation: The second row has a 0 at (1,1). Hence, the entire second row and the second column are set to 0.
• Input: matrix = [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
• Explanation: The second row has a 0 at (1,1). Hence, the entire second row and the second column are set to 0.
Approach: The solution involves using the first row and column to store the state of rows and columns that need to be zeroed. This allows the matrix to be modified in place without using additional space.
Observations:
• The matrix needs to be transformed in place without extra space.
• We can use the first row and column to store flags for marking rows and columns that need to be zeroed.
• Using a flag system for the first row and column will prevent overwriting important data during the transformation.
Steps:
• Traverse the matrix to check for zeros.
• Mark the first row and first column if they contain a zero.
• Use the first row and column to flag other rows and columns that contain zeros.
• Traverse the matrix again to apply the zeroing transformation.
• Handle the first row and column separately if they were marked as containing zeros.
Empty Inputs:
• If the matrix has only one row or one column, it must be handled appropriately.
Large Inputs:
• Consider cases where the matrix size is at the upper limit (200x200).
Special Values:
• Ensure that negative numbers and boundary values are handled correctly.
Constraints:
• The solution must be implemented in-place and must not use additional matrices for storage.
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
bool firstRowZero = false, firstColZero = false;
// Check if first row has a zero
for (int j = 0; j < n; j++) {
if (matrix[0][j] == 0) {
firstRowZero = true;
break;
}
}
// Check if first column has a zero
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) {
firstColZero = true;
break;
}
}
// Mark the rows and columns with zeros using the first row and column
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = matrix[0][j] = 0;
}
}
}
// Set the rows and columns marked as zero to zero
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
// Set the first row to zero if necessary
if (firstRowZero) {
for (int j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
// Set the first column to zero if necessary
if (firstColZero) {
for (int i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
}
1 : Function Declaration
void setZeroes(vector<vector<int>>& matrix) {
Declares a function `setZeroes` that takes a 2D vector `matrix` as input and modifies it in-place.
2 : Variable Initialization
int m = matrix.size(), n = matrix[0].size();
Initializes variables `m` and `n` to store the number of rows and columns in the matrix.
3 : Variable Initialization
bool firstRowZero = false, firstColZero = false;
Initializes boolean flags `firstRowZero` and `firstColZero` to track if the first row and column contain zeros.
4 : Loop Iteration
for (int j = 0; j < n; j++) {
if (matrix[0][j] == 0) {
firstRowZero = true;
break;
}
}
Iterates through the first row of the matrix and sets `firstRowZero` to `true` if a zero is found.
5 : Loop Iteration
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) {
firstColZero = true;
break;
}
}
Iterates through the first column of the matrix and sets `firstColZero` to `true` if a zero is found.
6 : Nested Loops
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = matrix[0][j] = 0;
}
}
}
Iterates through the matrix, excluding the first row and column. If a zero is found, marks the corresponding row and column using the first row and column as markers.
7 : Nested Loops
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
Iterates through the matrix again, setting elements to zero based on the markers in the first row and column.
8 : Conditional
if (firstRowZero) {
for (int j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
If the first row was originally zero, set all elements in the first row to zero.
9 : Conditional
if (firstColZero) {
for (int i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
If the first column was originally zero, set all elements in the first column to zero.
Best Case: O(m * n)
Average Case: O(m * n)
Worst Case: O(m * n)
Description: The time complexity is O(m * n) because we iterate through the entire matrix twice.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is O(1) because the solution is implemented in-place without additional storage.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus