-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetMatrixZeroes.java
292 lines (234 loc) · 8.92 KB
/
SetMatrixZeroes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package Algorithms.Matrix;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 10 March 2025
*
*/
public class SetMatrixZeroes {
public static void main(String[] args) {
int[][] matrix = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
/*
(0,0),(0,1),(0,2),(0,3),(0,4)
(1,0),(1,1),(1,2),(1,3),(1,4)
(2,0),(2,1),(2,2),(2,3),(2,4)
(3,0),34,1)3(4,23,(4,3),(4,4)
(4,0),(4,1),(4,2),(4,3),(4,4)
*/
System.out.println("setZeroes: ---------------------");
System.out.println("Before: ");
for (int i = 0; i < matrix.length; i++) System.out.println(Arrays.toString(matrix[i]));
setZeroes(matrix);
System.out.println("\nAfter: ");
for (int i = 0; i < matrix.length; i++) System.out.println(Arrays.toString(matrix[i]));
}
/**
* @TimeComplexity O(m*n)
* @SpaceComplexity O(1)
Initially:
0 1 2 7
3 4 5 0
1 3 1 5
1st Loop: Mark left and top
0 1 2 0
0 4 5 0
1 3 1 5
2nd Loop: Skip 1st row & col and mark all possible positions as 0's by comparing with 1st row & col
0 1 2 0
0 0 0 0
1 3 1 5
3rd Loop:
0 0 0 0
0 0 0 0
0 3 1 5
Similarly-----
Initially:
1 1 1 1
1 1 1 0
1 0 1 1
1 1 1 1
1st Loop: Mark left and top
1 0 1 0
0 1 1 0
0 0 1 1
1 1 1 1
2nd Loop: Skip 1st row & col and mark all possible positions as 0's by comparing with 1st row & col
1 0 1 0
0 0 0 0
0 0 0 0
1 0 1 0
3rd Loop: -- no loop here, as in 1st loop we didn't find any 0's in 1st row and 1st col
1 0 1 0
0 0 0 0
0 0 0 0
1 0 1 0
Instead of using extra space, we can mark the positions left and top that need to be zeroed out.
Then we can
*/
public static void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean isFirstRowHasZero = false, isFirstColHasZero = false;
// mark the positions that need to be zeroed out
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (matrix[r][c] == 0) {
if (r == 0) isFirstRowHasZero = true;
if (c == 0) isFirstColHasZero = true;
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
// zero out the marked positions except the first row and column
for (int r = 1; r < m; r++) {
for (int c = 1; c < n; c++) {
if (matrix[r][0] == 0 || matrix[0][c] == 0) matrix[r][c] = 0; // isTopRowZero || isLeftColZero
}
}
// handle the first row and column
if (isFirstRowHasZero) {
for (int c = 0; c < n; c++) matrix[0][c] = 0;
}
if (isFirstColHasZero) {
for (int r = 0; r < m; r++) matrix[r][0] = 0;
}
}
/**
* @TimeComplexity O(m*n)
* @SpaceComplexity O(1)
Same as above setZeroes() but instead use one variable for isRowZero
*/
public static void setZeroes2(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean isRowZero = false;
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (matrix[r][c] == 0) {
matrix[0][c] = 0; // mark the position that needs to be zeroed out
if (r > 0) matrix[r][0] = 0; // skipping matrix[0][0], mark the position that needs to be zeroed out
else isRowZero = true; // mark the first column that needs to be zeroed out
}
}
}
for (int r = 1; r < m; r++) {
for (int c = 1; c < n; c++) {
if (matrix[r][c] !=0 && (matrix[r][0] == 0 || matrix[0][c] == 0)) matrix[r][c] = 0;
}
}
if (matrix[0][0] == 0) {
for (int r = 0; r < m; r++) matrix[r][0] = 0;
}
if (isRowZero) {
for (int c = 0; c < n; c++) matrix[0][c] = 0;
}
}
/**
* @TimeComplexity O(m*n)
* @SpaceComplexity O(m+n)
*
* ___ [ ----- dummy row ------- ]
* | (0,0),(0,1),(0,2),(0,3),(0,4)
* | (1,0),(1,1),(1,2),(1,3),(1,4)
* dummy col (2,0),(2,1),(2,2),(2,3),(2,4)
* | (3,0),(3,1),(3,2),(3,3),(3,4)
* _|_ (4,0),(4,1),(4,2),(4,3),(4,4)
*
*
* ___[T f f T]
* T 0 1 2 0
* f 3 4 5 2
* _f_ 1 3 1 5
*
*/
public static void setZeroesUsingDummyRowAndCol(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
// mark the positions that need to be zeroed out
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (matrix[r][c] == 0) {
row[r] = true;
col[c] = true;
}
}
}
// zero out the marked positions
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (row[r] || col[c]) matrix[r][c] = 0;
}
}
}
// It's not working, but to understand the setZeros() method ---> cause, here we trav from i=0,j=0
// Here we mark t & l as they'll be 0's
// but as we start from i=0 j=0, we'll mark the 1st row & col as 0's that's why this setZeroesDummy() is not working
public void setZeroesDummy(int[][] matrix) {
for (int i=0; i<matrix.length; i++) {
for(int j=0; j<matrix[0].length; j++) {
if (matrix[i][j]==0) {
// mark t & l as they already visited
matrix[0][j]=0; //t
matrix[i][0]=0; //l
}
}
}
// left col --- set matched rows to 0 --- RIGHT 0's
for (int row=0; row<matrix.length; row++) {
if (matrix[row][0] == 0) {
for(int col=1; col<matrix[0].length; col++) matrix[row][col]=0;
}
}
// top row --- set matched cols to 0 --- DOWN 0's
for(int col=0; col<matrix[0].length; col++) {
if (matrix[0][col] == 0) { // 1st row
for (int row=1; row<matrix.length; row++) matrix[row][col]=0;
}
}
}
public static void setZeroesUsingList(int[][] matrix) {
List<List<Integer>> lst = new ArrayList<>();
for (int i=0; i<matrix.length; i++) {
for(int j=0; j<matrix[0].length; j++) {
if (matrix[i][j]==0) lst.add(Arrays.asList(i,j));
}
}
for (List<Integer> subLst: lst) setZeroes(matrix, subLst.get(0), subLst.get(1));
}
private static void setZeroes(int[][] matrix, int i, int j) {
// two loops
int a,b;
// 1st loop ---> keep row same and move l and r
for(a=i,b=j; a>=0; a--) matrix[a][b] = 0;
for(a=i,b=j; a<matrix.length; a++) matrix[a][b] = 0;
// 2nd loop ---> keep col and move t and d
for(a=i,b=j; b>=0; b--) matrix[a][b] = 0;
for(a=i,b=j; b<matrix[0].length; b++) matrix[a][b] = 0;
}
// NOT WORKING as -2^31 <= matrix[i][j] <= 2^31 - 1
public void setZeroesIntuition(int[][] matrix) {
for (int i=0; i<matrix.length; i++) {
for(int j=0; j<matrix[0].length; j++) {
if (matrix[i][j]==0) markPosition(matrix, i, j);
}
}
for (int i=0; i<matrix.length; i++) {
for(int j=0; j<matrix[0].length; j++) {
if (matrix[i][j]==-1) matrix[i][j]=0; // NOT WORKING as -2^31 <= matrix[i][j] <= 2^31 - 1
}
}
}
private void markPosition(int[][] matrix, int i, int j) {
// two loops
// NOTE: DON'T CHANGE 0'S THAT YOU SEE HERE
int a,b;
// 1st loop ---> keep row same and move l and r
for(a=i,b=j; a>=0; a--) if (matrix[a][b] != 0) matrix[a][b]=-1;
for(a=i,b=j; a<matrix.length; a++) if (matrix[a][b] != 0) matrix[a][b]=-1;
// 2nd loop ---> keep col and move t and d
for(a=i,b=j; b>=0; b--) if (matrix[a][b] != 0) matrix[a][b]=-1;
for(a=i,b=j; b<matrix[0].length; b++) if (matrix[a][b] != 0) matrix[a][b]=-1;
}
}