-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathRottingOranges.java
408 lines (363 loc) · 12.3 KB
/
RottingOranges.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package LeetCodeJava.BFS;
// https://leetcode.com/problems/rotting-oranges/
/**
* 994. Rotting Oranges
* Solved
* Medium
* Topics
* Companies
* You are given an m x n grid where each cell can have one of three values:
*
* 0 representing an empty cell,
* 1 representing a fresh orange, or
* 2 representing a rotten orange.
* Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.
*
* Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
*
*
*
* Example 1:
*
*
* Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
* Output: 4
* Example 2:
*
* Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
* Output: -1
* Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
* Example 3:
*
* Input: grid = [[0,2]]
* Output: 0
* Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
*
*
* Constraints:
*
* m == grid.length
* n == grid[i].length
* 1 <= m, n <= 10
* grid[i][j] is 0, 1, or 2.
*/
import java.util.*;
public class RottingOranges {
// TODO : fix below
// VO
// IDEA : BFS
// public int orangesRotting(int[][] grid) {
//
// // edge
// if(grid.length == 0 || grid[0].length == 0){
// return 0;
// }
//
// if(grid.length == 1 && grid[0].length == 1){
// if(grid[0][0] == 2){
// return -1;
// }
// return 0; // ??
// }
//
// int l = grid.length;
// int w = grid[0].length;
//
// int freshCnt = 0;
// int rottenCnt = 0;
//
// List<List<Integer>> rottenList = new ArrayList<>();
//
// // get cnt of `fresh` orange
// for(int i = 0; i < l; i++){
// for(int j = 0; j < w; j++){
// if(grid[i][j] == 1){
// freshCnt += 1;
// }else if (grid[i][j] == 2){
// rottenCnt += 1;
// List<Integer> tmp = new ArrayList<>();
// tmp.add(j);
// tmp.add(i);
// rottenList.add(tmp);
// }
// }
// }
//
// if(freshCnt == 0){
// return 0;
// }
//
// int minutes = 0;
//
// Queue<List<Integer>> q = new LinkedList<>();
// for(List<Integer> x: rottenList){
// q.add(x);
// }
//
// while(!q.isEmpty() || freshCnt <= 0){
// List<Integer> cur = q.poll();
// int x = cur.get(0);
// int y = cur.get(1);
//
// //grid[y][x] = 2; // make as `rotten`
//
// int[][] dirs = new int[][] { {0,1}, {0,-1}, {1,0}, {-1,0} };
//
// for(int[] d: dirs){
// int x_ = x + d[0];
// int y_ = y + d[1];
// if(x_ < 0 || x_ >= w || y < 0 || y_ >= l || grid[y_][x_] == 0){
// continue;
// }
// List<Integer> cur2 = new ArrayList<>();
// cur2.add(x_);
// cur2.add(y_);
//
// q.add(cur2);
//
// if(grid[y_][x_] == 1){
// grid[y_][x_] = 2; // make as `rotten`
// freshCnt -= 1;
// }
// }
//
// minutes += 1;
// }
//
// return minutes;
// }
// TODO : fix below
// V0-1
// IDEA : BFS (fixed by gpt)
public int orangesRotting_0_1(int[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int rows = grid.length;
int cols = grid[0].length;
int freshCnt = 0;
Queue<int[]> queue = new LinkedList<>();
// Initialize queue with all initially rotten oranges and count fresh oranges
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 2) {
queue.offer(new int[] { i, j });
} else if (grid[i][j] == 1) {
freshCnt++;
}
}
}
// If no fresh oranges exist, return 0
if (freshCnt == 0) {
return 0;
}
int time = 0;
int[][] directions = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
// Perform BFS
while (!queue.isEmpty()) {
int size = queue.size();
boolean anyRotten = false;
for (int i = 0; i < size; i++) {
int[] cell = queue.poll();
int x = cell[0];
int y = cell[1];
for (int[] dir : directions) {
int newX = x + dir[0];
int newY = y + dir[1];
if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && grid[newX][newY] == 1) {
grid[newX][newY] = 2; // Mark as rotten
queue.offer(new int[] { newX, newY });
freshCnt--;
anyRotten = true;
}
}
}
if (anyRotten) {
time++;
}
}
return freshCnt == 0 ? time : -1; // If fresh oranges remain, return -1
}
// V1-1
// https://neetcode.io/problems/rotting-fruit
// IDEA: BFS
public int orangesRotting_1_1(int[][] grid) {
Queue<int[]> q = new ArrayDeque<>();
int fresh = 0;
int time = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[0].length; c++) {
if (grid[r][c] == 1) {
fresh++;
}
if (grid[r][c] == 2) {
q.offer(new int[]{r, c});
}
}
}
int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
while (fresh > 0 && !q.isEmpty()) {
int length = q.size();
for (int i = 0; i < length; i++) {
int[] curr = q.poll();
int r = curr[0];
int c = curr[1];
for (int[] dir : directions) {
int row = r + dir[0];
int col = c + dir[1];
if (row >= 0 && row < grid.length &&
col >= 0 && col < grid[0].length &&
grid[row][col] == 1) {
grid[row][col] = 2;
q.offer(new int[]{row, col});
fresh--;
}
}
}
time++;
}
return fresh == 0 ? time : -1;
}
// V1-2
// https://neetcode.io/problems/rotting-fruit
// IDEA: BFS (No Queue)
public int orangesRotting_1_2(int[][] grid) {
int ROWS = grid.length, COLS = grid[0].length;
int fresh = 0, time = 0;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
if (grid[r][c] == 1) fresh++;
}
}
int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
while (fresh > 0) {
boolean flag = false;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
if (grid[r][c] == 2) {
for (int[] d : directions) {
int row = r + d[0], col = c + d[1];
if (row >= 0 && col >= 0 &&
row < ROWS && col < COLS &&
grid[row][col] == 1) {
grid[row][col] = 3;
fresh--;
flag = true;
}
}
}
}
}
if (!flag) return -1;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
if (grid[r][c] == 3) grid[r][c] = 2;
}
}
time++;
}
return time;
}
// V2
// IDEA : BFS
// https://leetcode.com/problems/rotting-oranges/editorial/
class Pair<U, V> {
U key;
V value;
Pair(U key, V value) {
this.key = key;
this.value = value;
}
public U getKey() {
return this.key;
}
public V getValue() {
return this.value;
}
}
public int orangesRotting_2(int[][] grid) {
Queue<Pair<Integer, Integer>> queue = new ArrayDeque();
// Step 1). build the initial set of rotten oranges
int freshOranges = 0;
int ROWS = grid.length, COLS = grid[0].length;
for (int r = 0; r < ROWS; ++r)
for (int c = 0; c < COLS; ++c)
if (grid[r][c] == 2)
queue.offer(new Pair(r, c));
else if (grid[r][c] == 1)
freshOranges++;
// Mark the round / level, _i.e_ the ticker of timestamp
queue.offer(new Pair(-1, -1));
// Step 2). start the rotting process via BFS
int minutesElapsed = -1;
int[][] directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
while (!queue.isEmpty()) {
Pair<Integer, Integer> p = queue.poll();
int row = p.getKey();
int col = p.getValue();
if (row == -1) {
// We finish one round of processing
minutesElapsed++;
// to avoid the endless loop
if (!queue.isEmpty())
queue.offer(new Pair(-1, -1));
} else {
// this is a rotten orange
// then it would contaminate its neighbors
for (int[] d : directions) {
int neighborRow = row + d[0];
int neighborCol = col + d[1];
if (neighborRow >= 0 && neighborRow < ROWS &&
neighborCol >= 0 && neighborCol < COLS) {
if (grid[neighborRow][neighborCol] == 1) {
// this orange would be contaminated
grid[neighborRow][neighborCol] = 2;
freshOranges--;
// this orange would then contaminate other oranges
queue.offer(new Pair(neighborRow, neighborCol));
}
}
}
}
}
// return elapsed minutes if no fresh orange left
return freshOranges == 0 ? minutesElapsed : -1;
}
// V3
// IDEA : in place BFS
// https://leetcode.com/problems/rotting-oranges/editorial/
// run the rotting process, by marking the rotten oranges with the timestamp
public boolean runRottingProcess(int timestamp, int[][] grid, int ROWS, int COLS) {
int[][] directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
// flag to indicate if the rotting process should be continued
boolean toBeContinued = false;
for (int row = 0; row < ROWS; ++row)
for (int col = 0; col < COLS; ++col)
if (grid[row][col] == timestamp)
// current contaminated cell
for (int[] d : directions) {
int nRow = row + d[0], nCol = col + d[1];
if (nRow >= 0 && nRow < ROWS && nCol >= 0 && nCol < COLS)
if (grid[nRow][nCol] == 1) {
// this fresh orange would be contaminated next
grid[nRow][nCol] = timestamp + 1;
toBeContinued = true;
}
}
return toBeContinued;
}
public int orangesRotting_3(int[][] grid) {
int ROWS = grid.length, COLS = grid[0].length;
int timestamp = 2;
while (runRottingProcess(timestamp, grid, ROWS, COLS))
timestamp++;
// end of process, to check if there are still fresh oranges left
for (int[] row : grid)
for (int cell : row)
// still got a fresh orange left
if (cell == 1)
return -1;
// return elapsed minutes if no fresh orange left
return timestamp - 2;
}
}