-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRottingOranges.java
More file actions
312 lines (251 loc) · 8.91 KB
/
RottingOranges.java
File metadata and controls
312 lines (251 loc) · 8.91 KB
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
package Algorithms.Graphs;
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
/**
[2,1,1]
[0,1,1]
[1,0,1]
THOUGHTS:
---------
1) Rots horizontally & vertically but not diagonally
2) Some oranges don't rot at all --> return -1
3) In some cases, all the oranges are already rotten --> return 0
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 16 Jan 2025
* same as {@link Algorithms.Graphs.NearestExitFromEntranceInMaze}
*/
@SuppressWarnings("unused")
public class RottingOranges {
public static void main(String[] args) {
int[][] grid = {{2,1,1},{1,1,0},{0,1,1}};
System.out.println("orangesRotting(grid): " + orangesRottingMyApproach(grid));
}
/**
APPROACH: -- bfs
---------
1) Count num of 1s
2) Store all 2s in queue
3) Do while(!q.isEmpty()) with for(q size) to trav each level
4) Return the level if no 1s left or if 1s left then return -1
*/
public static int orangesRottingMyApproach(int[][] grid) {
int m=grid.length, n=grid[0].length, ones=0, level=0, dirs[][]={{0,1}, {1,0}, {0,-1}, {-1,0}};
Queue<int[]> q = new LinkedList<>();
for(int r=0; r<m; r++) {
for (int c=0; c<n; c++) {
if(grid[r][c]==1) ones++;
else if(grid[r][c]==2) q.add(new int[]{r,c});
}
}
if(ones == 0) return 0;
while(!q.isEmpty()) {
int size = q.size();
for(int i=0; i<size; i++) {
int[] curr = q.poll();
int r=curr[0], c=curr[1];
// next
for(int[] dir: dirs) {
int nr=r+dir[0], nc=c+dir[1];
if(isOutOfBounds(m,n,nr,nc) || grid[nr][nc]!=1) continue;
q.add(new int[]{nr, nc});
grid[nr][nc]=2;
ones--;
// System.out.printf("r:%s, c:%s, nr:%s, nc:%s, ones:%s, level:%s \n", r, c, nr, nc, ones, level);
if(ones==0) return level+1;
}
}
level++;
// System.out.println();
}
return -1;
}
private static boolean isOutOfBounds(int m, int n, int i, int j) {
return i<0 || j<0 || i>=m || j>=n;
}
public int orangesRotting(int[][] grid) {
int freshCount = 0;
int n = grid.length;
int m = grid[0].length;
int[][] dirs = new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
Queue<int[]> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1)
freshCount++;
else if (grid[i][j] == 2)
q.add(new int[] { i, j });
}
}
int level=0;
while (!q.isEmpty() && freshCount > 0) {
int size = q.size();
while(size-- > 0) { // decrement size
int[] cur = q.poll();
for(int[] dir :dirs){
int new_x=dir[0]+cur[0];
int new_y=dir[1]+cur[1];
if(new_x>=0 && new_x<n && new_y>=0 && new_y<m && grid[new_x][new_y]==1){
grid[new_x][new_y]=2;
freshCount--;
q.add(new int[]{new_x,new_y});
}
}
}
level++;
}
return freshCount == 0 ? level : -1;
}
public int orangesRotting2(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
Queue<int[]> queue = new ArrayDeque<>();
int goodOrange = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == 1){
goodOrange++;
}
if(grid[i][j] == 2){
queue.offer(new int[]{i, j});
}
}
}
int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int count = 0;
int time = 0;
boolean[][] visited = new boolean[m][n];
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
int[] current = queue.poll();
int currentX = current[0];
int currentY = current[1];
for(int[] dir : directions){
int nextX = currentX + dir[0];
int nextY = currentY + dir[1];
if(nextX >= 0 && nextX < m && nextY >= 0 && nextY < n && grid[nextX][nextY] == 1 && !visited[nextX][nextY]){
queue.offer(new int[]{nextX, nextY});
visited[nextX][nextY] = true;
count++;
}
}
}
if(queue.size() > 0){
time++;
}
}
return count == goodOrange ? time : -1;
}
public static boolean isValid(int rowVal, int colVal, int rsize, int csize) {return rowVal >= 0 && rowVal < rsize && colVal >= 0 && colVal < csize;}
static class Pair { int row; int col; Pair(int row, int col) { this.row = row; this.col = col; } }
public static int orangesRotting3(int[][] grid) {
int[] rOffset = {-1, 0, 1, 0};
int[] cOffset = {0, 1, 0, -1};
int rsize = grid.length;
int csize = grid[0].length;
Queue<Pair> queue = new LinkedList<>();
boolean isFresh = false;
for (int i = 0; i < rsize; i++) {
for (int j = 0; j < csize; j++) {
if (grid[i][j] == 1) {
isFresh = true;
}
}
}
if (!isFresh) {
return 0;
}
int time = 0;
for (int i = 0; i < rsize; i++) {
for (int j = 0; j < csize; j++) {
if (grid[i][j] == 2) {
queue.offer(new Pair(i, j));
}
}
}
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Pair curr = queue.poll();
for (int j = 0; j < 4; j++) {
int rowVal = curr.row + rOffset[j];
int colVal = curr.col + cOffset[j];
if (isValid(rowVal, colVal, rsize, csize) && grid[rowVal][colVal] == 1) {
grid[rowVal][colVal] = 2;
queue.offer(new Pair(rowVal, colVal));
}
}
}
if (!queue.isEmpty()) {
time++;
}
}
for (int i = 0; i < rsize; i++) {
for (int j = 0; j < csize; j++) {
if (grid[i][j] == 1) {
return -1;
}
}
}
return time;
}
public static int orangesRotting4(int[][] grid) {
int rows = grid.length, cols = grid[0].length, count = 0, fresh = 0, minutes = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1) fresh++;
if (grid[i][j] == 2) count++;
}
}
while (count > 0 && fresh > 0) {
minutes++;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 2) {
if (i - 1 >= 0 && grid[i - 1][j] == 1) {
grid[i - 1][j] = 2;
fresh--;
}
if (i + 1 < rows && grid[i + 1][j] == 1) {
grid[i + 1][j] = 2;
fresh--;
}
if (j - 1 >= 0 && grid[i][j - 1] == 1) {
grid[i][j - 1] = 2;
fresh--;
}
if (j + 1 < cols && grid[i][j + 1] == 1) {
grid[i][j + 1] = 2;
fresh--;
}
}
}
}
count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1) fresh++;
if (grid[i][j] == 2) count++;
}
}
}
return fresh == 0 ? minutes : -1;
}
public int orangesRottingMyApproachOld(int[][] grid) {
int mins = 0;
for (int[] r: grid) {
for (int c: r) {
if (c==2) return -1; // all oranges cannot rot
}
}
// at last
for (int[] r: grid) {
for (int c: r) {
if (c==1) return -1; // all oranges cannot rot
}
}
return mins;
}
private void dfs() {}
}