-
Notifications
You must be signed in to change notification settings - Fork 43
/
RottingOranges.java
310 lines (286 loc) · 9.47 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
package LeetCodeJava.BFS;
// https://leetcode.com/problems/rotting-oranges/
import java.util.*;
public class RottingOranges {
// TODO : fix below
// VO
// IDRA : BFS
// public int orangesRotting(int[][] grid) {
//
// class Pair<U, V> {
// U key;
// V value;
//
// Pair(U key, V value) {
// this.key = key;
// this.value = value;
// }
//
// U getKey() {
// return this.key;
// }
//
// V getValue() {
// return this.value;
// }
//
// }
//
// int len = grid.length;
// int width = grid[0].length;
//
// int ans = 0;
// int fresh_cnt = 0;
//
// int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
// Queue<Pair> q = new LinkedList<>();
//
// // collect rotting orange
// for (int i = 0; i < width; i++) {
// for (int j = 0; j < len; j++) {
// if (grid[j][i] == 2) {
// q.add(new Pair(i, j));
// } else if (grid[j][i] == 1) {
// fresh_cnt += 1;
// }
// }
// }
//
// if (fresh_cnt == 0) {
// return 0; // ?
// }
//
// // bfs
// while (!q.isEmpty()) {
//
// Pair p = q.poll();
// int x = (int) p.getKey();
// int y = (int) p.getValue();
//
// boolean finish_cycle = false;
//
// for (int[] dir : dirs) {
//
// int dx = dir[0];
// int dy = dir[1];
//
// int new_x = x + dx;
// int new_y = y + dy;
//
// if (new_x >= 0 && new_x < width && new_y >= 0 && new_y < len) {
// if (grid[new_y][new_x] == 1) {
// q.add(new Pair(new_x, new_y));
// grid[new_y][new_x] = 2; // become rotting orange
// fresh_cnt -= 1;
//
// }
// }
//
// if (fresh_cnt == 0) {
// return ans;
// }
//
// finish_cycle = true;
// }
//
// if (finish_cycle) {
// ans += 1;
// }
// }
//
// System.out.println("fresh_cnt = " + fresh_cnt + " ans = " + ans);
// return fresh_cnt == 0 ? ans : -1;
// }
// TODO : fix below
// V0
// IDEA : BFS
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Breadth-First-Search/rotting-oranges.py
// public int orangesRotting(int[][] grid) {
//
// class Pair<U, V, W> {
//
// U key;
// V value;
// W value2;
//
// Pair(U key, V value, W value2) {
// this.key = key;
// this.value = value;
// this.value2 = value2;
// }
//
// public U getKey() {
// return this.key;
// }
//
// public V getValue() {
// return this.value;
// }
//
// public W getValue2() {
// return this.value2;
// }
//
// }
//
// // Queue<Pair<Integer, Integer>> queue = new ArrayDeque();
// int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
// int fresh = 0;
// Queue<Pair<Integer, Integer, Integer>> rotting = new LinkedList<>();
// HashSet<String> set = new HashSet<>();
//
// int _len = grid.length;
// int _width = grid[0].length;
//
// for (int i = 0; i < _width; i++) {
// for (int j = 0; j < _len; j++) {
// if (grid[j][i] == 2) {
// // TODO : check difference : offer VS add
// //rotting.offer(new Pair<>(i, j));
// rotting.add(new Pair<>(i, j, 0));
// }
// if (grid[j][i] == 1) {
// fresh += 1;
// }
// }
// }
//
// if (fresh == 0) {
// return 0;
// }
//
// // bfs
// while (!rotting.isEmpty()) {
//
// Pair<Integer, Integer, Integer> _cur = rotting.poll();
// int x = _cur.getKey();
// int y = _cur.getValue();
// int _time = _cur.getValue2();
//
// // go 4 directions
// for (int[] dir : dirs) {
// int dx = dir[0];
// int dy = dir[1];
// int _x = x + dx;
// int _y = y + dy;
// String idx = String.valueOf(x) + "-" + String.valueOf(y); // to avoid redo op (fresh -= 1, rotting.add) "visited" points
// if (_x >= 0 && x < _width - 1 && _y >= 0 && _y < _len - 1) {
// if (grid[_y][_x] == 1 && !set.contains(idx)){
// grid[_y][_x] = 2;
// fresh -= 1;
// set.add(idx);
// System.out.println(">>> fresh = " + fresh + " idx = " + idx);
// if (fresh == 0) {
// return _time+1;
// }
// rotting.add(new Pair<>(_x, _y, _time+1));
// }
// }
// }
// }
//
// return fresh == 0 ? 0 : -1;
// }
// V1
// 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_1(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;
}
// V2
// 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_2(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;
}
}