|
| 1 | +--- |
| 2 | +id: set-matrix-zeros |
| 3 | +title: Set Matrix Zeros |
| 4 | +difficulty: Medium |
| 5 | +sidebar_label: 0073-SetMatrixZeros |
| 6 | +tags: |
| 7 | + - Array |
| 8 | + - Hash Table |
| 9 | + - Matrix |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 15 | +| :---------------- | :------------ | :--------------- | |
| 16 | +| [Set Matrix Zeros](https://leetcode.com/problems/set-matrix-zeroes/description/) | [Set Matrix Zeros Solution on LeetCode](https://leetcode.com/problems/set-matrix-zeroes/solutions/) | [Leetcode Profile](https://leetcode.com/u/debangi_29/) | |
| 17 | + |
| 18 | +## Problem Description |
| 19 | + |
| 20 | +Given an $m \times n$ integer matrix matrix, if an element is 0, set its entire row and column to 0's. |
| 21 | + |
| 22 | +You must do it in place. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +#### Example 1: |
| 29 | + |
| 30 | +**Input**: |
| 31 | +``` |
| 32 | +Matrix = [[1,1,1],[1,0,1],[1,1,1]] |
| 33 | +``` |
| 34 | + |
| 35 | +**Output**: |
| 36 | +``` |
| 37 | +[[1,0,1],[0,0,0],[1,0,1]] |
| 38 | +``` |
| 39 | + |
| 40 | +#### Example 2: |
| 41 | +**Input**: |
| 42 | +``` |
| 43 | +matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] |
| 44 | +``` |
| 45 | + |
| 46 | +**Output**: |
| 47 | +``` |
| 48 | +[[0,0,0,0],[0,4,5,0],[0,3,1,0]] |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +### Constraints |
| 53 | + |
| 54 | +- $m = \text {matrix.length}$ |
| 55 | +- $n = \text {matrix[0].length}$ |
| 56 | +- $1 \leq m, n \leq 200$ |
| 57 | +- $-2^{31} \leq \text {matrix[i][j]} \leq 2^{31} - 1$ |
| 58 | + |
| 59 | +### Approach |
| 60 | + |
| 61 | +The steps are as follows: |
| 62 | + |
| 63 | +- First, we will traverse the matrix and mark the proper cells of 1st row and 1st column with 0 accordingly. The marking will be like this: if cell(i, j) contains 0, we will mark the i-th row i.e. matrix[i][0] with 0 and we will mark j-th column i.e. matrix[0][j] with 0. |
| 64 | +If i is 0, we will mark matrix[0][0] with 0 but if j is 0, we will mark the col0 variable with 0 instead of marking matrix[0][0] again. |
| 65 | + |
| 66 | +- After step 1 is completed, we will modify the cells from (1,1) to (n-1, m-1) using the values from the 1st row, 1st column, and col0 variable. |
| 67 | +We will not modify the 1st row and 1st column of the matrix here as the modification of the rest of the matrix(i.e. From (1,1) to (n-1, m-1)) is dependent on that row and column. |
| 68 | + |
| 69 | +- Finally, we will change the 1st row and column using the values from matrix[0][0] and col0 variable. Here also we will change the row first and then the column. |
| 70 | +If matrix[0][0] = 0, we will change all the elements from the cell (0,1) to (0, m-1), to 0. |
| 71 | +If col0 = 0, we will change all the elements from the cell (0,0) to (n-1, 0), to 0. |
| 72 | + |
| 73 | +### Solution Code |
| 74 | + |
| 75 | +#### Python |
| 76 | + |
| 77 | +``` |
| 78 | +class Solution: |
| 79 | + def setZeroes(self, matrix: List[List[int]]) -> None: |
| 80 | + m = len(matrix) |
| 81 | + n = len(matrix[0]) |
| 82 | + shouldFillFirstRow = 0 in matrix[0] |
| 83 | + shouldFillFirstCol = 0 in list(zip(*matrix))[0] |
| 84 | +
|
| 85 | + # Store the information in the first row and the first column. |
| 86 | + for i in range(1, m): |
| 87 | + for j in range(1, n): |
| 88 | + if matrix[i][j] == 0: |
| 89 | + matrix[i][0] = 0 |
| 90 | + matrix[0][j] = 0 |
| 91 | +
|
| 92 | + # Fill 0s for the matrix except the first row and the first column. |
| 93 | + for i in range(1, m): |
| 94 | + for j in range(1, n): |
| 95 | + if matrix[i][0] == 0 or matrix[0][j] == 0: |
| 96 | + matrix[i][j] = 0 |
| 97 | +
|
| 98 | + # Fill 0s for the first row if needed. |
| 99 | + if shouldFillFirstRow: |
| 100 | + matrix[0] = [0] * n |
| 101 | +
|
| 102 | + # Fill 0s for the first column if needed. |
| 103 | + if shouldFillFirstCol: |
| 104 | + for row in matrix: |
| 105 | + row[0] = 0 |
| 106 | +
|
| 107 | +``` |
| 108 | + |
| 109 | +#### Java |
| 110 | + |
| 111 | +``` |
| 112 | +class Solution { |
| 113 | + public void setZeroes(int[][] matrix) { |
| 114 | + final int m = matrix.length; |
| 115 | + final int n = matrix[0].length; |
| 116 | + boolean shouldFillFirstRow = false; |
| 117 | + boolean shouldFillFirstCol = false; |
| 118 | +
|
| 119 | + for (int j = 0; j < n; ++j) |
| 120 | + if (matrix[0][j] == 0) { |
| 121 | + shouldFillFirstRow = true; |
| 122 | + break; |
| 123 | + } |
| 124 | +
|
| 125 | + for (int i = 0; i < m; ++i) |
| 126 | + if (matrix[i][0] == 0) { |
| 127 | + shouldFillFirstCol = true; |
| 128 | + break; |
| 129 | + } |
| 130 | +
|
| 131 | + // Store the information in the first row and the first column. |
| 132 | + for (int i = 1; i < m; ++i) |
| 133 | + for (int j = 1; j < n; ++j) |
| 134 | + if (matrix[i][j] == 0) { |
| 135 | + matrix[i][0] = 0; |
| 136 | + matrix[0][j] = 0; |
| 137 | + } |
| 138 | +
|
| 139 | + // Fill 0s for the matrix except the first row and the first column. |
| 140 | + for (int i = 1; i < m; ++i) |
| 141 | + for (int j = 1; j < n; ++j) |
| 142 | + if (matrix[i][0] == 0 || matrix[0][j] == 0) |
| 143 | + matrix[i][j] = 0; |
| 144 | +
|
| 145 | + // Fill 0s for the first row if needed. |
| 146 | + if (shouldFillFirstRow) |
| 147 | + for (int j = 0; j < n; ++j) |
| 148 | + matrix[0][j] = 0; |
| 149 | +
|
| 150 | + // Fill 0s for the first column if needed. |
| 151 | + if (shouldFillFirstCol) |
| 152 | + for (int i = 0; i < m; ++i) |
| 153 | + matrix[i][0] = 0; |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +#### C++ |
| 159 | + |
| 160 | +``` |
| 161 | +class Solution { |
| 162 | + public: |
| 163 | + void setZeroes(vector<vector<int>>& matrix) { |
| 164 | + const int m = matrix.size(); |
| 165 | + const int n = matrix[0].size(); |
| 166 | + bool shouldFillFirstRow = false; |
| 167 | + bool shouldFillFirstCol = false; |
| 168 | +
|
| 169 | + for (int j = 0; j < n; ++j) |
| 170 | + if (matrix[0][j] == 0) { |
| 171 | + shouldFillFirstRow = true; |
| 172 | + break; |
| 173 | + } |
| 174 | +
|
| 175 | + for (int i = 0; i < m; ++i) |
| 176 | + if (matrix[i][0] == 0) { |
| 177 | + shouldFillFirstCol = true; |
| 178 | + break; |
| 179 | + } |
| 180 | +
|
| 181 | + // Store the information in the first row and the first column. |
| 182 | + for (int i = 1; i < m; ++i) |
| 183 | + for (int j = 1; j < n; ++j) |
| 184 | + if (matrix[i][j] == 0) { |
| 185 | + matrix[i][0] = 0; |
| 186 | + matrix[0][j] = 0; |
| 187 | + } |
| 188 | +
|
| 189 | + // Fill 0s for the matrix except the first row and the first column. |
| 190 | + for (int i = 1; i < m; ++i) |
| 191 | + for (int j = 1; j < n; ++j) |
| 192 | + if (matrix[i][0] == 0 || matrix[0][j] == 0) |
| 193 | + matrix[i][j] = 0; |
| 194 | +
|
| 195 | + // Fill 0s for the first row if needed. |
| 196 | + if (shouldFillFirstRow) |
| 197 | + for (int j = 0; j < n; ++j) |
| 198 | + matrix[0][j] = 0; |
| 199 | +
|
| 200 | + // Fill 0s for the first column if needed. |
| 201 | + if (shouldFillFirstCol) |
| 202 | + for (int i = 0; i < m; ++i) |
| 203 | + matrix[i][0] = 0; |
| 204 | + } |
| 205 | +}; |
| 206 | +
|
| 207 | +``` |
| 208 | + |
| 209 | +### Conclusion |
| 210 | + |
| 211 | +- Time Complexity: $O(2*(N*M))$, where N = no. of rows in the matrix and M = no. of columns in the matrix. |
| 212 | + |
| 213 | + Reason: In this approach, we are also traversing the entire matrix 2 times and each traversal is taking $O(N*M)$ time complexity. |
| 214 | + |
| 215 | +- Space Complexity: $O(1)$ as we are not using any extra space. |
0 commit comments