Skip to content

Commit d854016

Browse files
committed
Added solution for leetcode 73
1 parent 6bdb820 commit d854016

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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 x 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+
Matrix = [[1,1,1],[1,0,1],[1,1,1]]
32+
33+
**Output**: [[1,0,1],[0,0,0],[1,0,1]]
34+
35+
#### Example 2:
36+
**Input**: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
37+
38+
**Output**: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
39+
40+
41+
### Constraints
42+
43+
- m == matrix.length
44+
- n == matrix[0].length
45+
- 1 <= m, n <= 200
46+
- -2^31 <= matrix[i][j] <= 2^31 - 1
47+
48+
### Approach
49+
50+
The steps are as follows:
51+
52+
- 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.
53+
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.
54+
55+
- 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.
56+
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.
57+
58+
- 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.
59+
If matrix[0][0] = 0, we will change all the elements from the cell (0,1) to (0, m-1), to 0.
60+
If col0 = 0, we will change all the elements from the cell (0,0) to (n-1, 0), to 0.
61+
62+
### Solution Code
63+
64+
#### Python
65+
66+
```
67+
class Solution:
68+
def setZeroes(self, matrix: List[List[int]]) -> None:
69+
m = len(matrix)
70+
n = len(matrix[0])
71+
shouldFillFirstRow = 0 in matrix[0]
72+
shouldFillFirstCol = 0 in list(zip(*matrix))[0]
73+
74+
# Store the information in the first row and the first column.
75+
for i in range(1, m):
76+
for j in range(1, n):
77+
if matrix[i][j] == 0:
78+
matrix[i][0] = 0
79+
matrix[0][j] = 0
80+
81+
# Fill 0s for the matrix except the first row and the first column.
82+
for i in range(1, m):
83+
for j in range(1, n):
84+
if matrix[i][0] == 0 or matrix[0][j] == 0:
85+
matrix[i][j] = 0
86+
87+
# Fill 0s for the first row if needed.
88+
if shouldFillFirstRow:
89+
matrix[0] = [0] * n
90+
91+
# Fill 0s for the first column if needed.
92+
if shouldFillFirstCol:
93+
for row in matrix:
94+
row[0] = 0
95+
96+
```
97+
98+
#### Java
99+
100+
```
101+
class Solution {
102+
public void setZeroes(int[][] matrix) {
103+
final int m = matrix.length;
104+
final int n = matrix[0].length;
105+
boolean shouldFillFirstRow = false;
106+
boolean shouldFillFirstCol = false;
107+
108+
for (int j = 0; j < n; ++j)
109+
if (matrix[0][j] == 0) {
110+
shouldFillFirstRow = true;
111+
break;
112+
}
113+
114+
for (int i = 0; i < m; ++i)
115+
if (matrix[i][0] == 0) {
116+
shouldFillFirstCol = true;
117+
break;
118+
}
119+
120+
// Store the information in the first row and the first column.
121+
for (int i = 1; i < m; ++i)
122+
for (int j = 1; j < n; ++j)
123+
if (matrix[i][j] == 0) {
124+
matrix[i][0] = 0;
125+
matrix[0][j] = 0;
126+
}
127+
128+
// Fill 0s for the matrix except the first row and the first column.
129+
for (int i = 1; i < m; ++i)
130+
for (int j = 1; j < n; ++j)
131+
if (matrix[i][0] == 0 || matrix[0][j] == 0)
132+
matrix[i][j] = 0;
133+
134+
// Fill 0s for the first row if needed.
135+
if (shouldFillFirstRow)
136+
for (int j = 0; j < n; ++j)
137+
matrix[0][j] = 0;
138+
139+
// Fill 0s for the first column if needed.
140+
if (shouldFillFirstCol)
141+
for (int i = 0; i < m; ++i)
142+
matrix[i][0] = 0;
143+
}
144+
}
145+
```
146+
147+
#### C++
148+
149+
```
150+
class Solution {
151+
public:
152+
void setZeroes(vector<vector<int>>& matrix) {
153+
const int m = matrix.size();
154+
const int n = matrix[0].size();
155+
bool shouldFillFirstRow = false;
156+
bool shouldFillFirstCol = false;
157+
158+
for (int j = 0; j < n; ++j)
159+
if (matrix[0][j] == 0) {
160+
shouldFillFirstRow = true;
161+
break;
162+
}
163+
164+
for (int i = 0; i < m; ++i)
165+
if (matrix[i][0] == 0) {
166+
shouldFillFirstCol = true;
167+
break;
168+
}
169+
170+
// Store the information in the first row and the first column.
171+
for (int i = 1; i < m; ++i)
172+
for (int j = 1; j < n; ++j)
173+
if (matrix[i][j] == 0) {
174+
matrix[i][0] = 0;
175+
matrix[0][j] = 0;
176+
}
177+
178+
// Fill 0s for the matrix except the first row and the first column.
179+
for (int i = 1; i < m; ++i)
180+
for (int j = 1; j < n; ++j)
181+
if (matrix[i][0] == 0 || matrix[0][j] == 0)
182+
matrix[i][j] = 0;
183+
184+
// Fill 0s for the first row if needed.
185+
if (shouldFillFirstRow)
186+
for (int j = 0; j < n; ++j)
187+
matrix[0][j] = 0;
188+
189+
// Fill 0s for the first column if needed.
190+
if (shouldFillFirstCol)
191+
for (int i = 0; i < m; ++i)
192+
matrix[i][0] = 0;
193+
}
194+
};
195+
196+
```
197+
198+
### Conclusion
199+
200+
- Time Complexity: $O(2*(N*M))$, where N = no. of rows in the matrix and M = no. of columns in the matrix.
201+
202+
Reason: In this approach, we are also traversing the entire matrix 2 times and each traversal is taking $O(N*M)$ time complexity.
203+
204+
- Space Complexity: $O(1)$ as we are not using any extra space.

0 commit comments

Comments
 (0)