Repository navigation
Expand file tree
/
Copy pathRottingOranges.java
More file actions
78 lines (65 loc) Β· 2.5 KB
/
Copy pathRottingOranges.java
File metadata and controls
78 lines (65 loc) Β· 2.5 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
package bfs.oranges;
import annotation.BreadthFirstSearch;
import java.util.ArrayList;
import java.util.List;
/**
* Time Complexity: O(m * n) <br />
* Space Complexity: O(m * n) <br />
*
* @see <a href="https://leetcode.com/problems/rotting-oranges/">994. Rotting Oranges</a>
*/
@BreadthFirstSearch
public class RottingOranges {
static final int ROTTEN = 2;
static final int FRESH = 1;
private static final int[][] DIRECTIONS = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
/**
* @param grid Constraints: m == grid.length, n == grid[0].length, 1 <= m, n <= 10
* @return Return the minimum number of minutes that must elapse until no cell has a fresh orange.
* If this is impossible, return -1.
*/
public int orangesRotting(int[][] grid) {
List<Cell> frontier = new ArrayList<>();
int countFresh = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == ROTTEN) {
frontier.add(new Cell(i, j));
} else if (grid[i][j] == FRESH) {
countFresh++;
}
}
}
int countMinutes = 0;
while (countFresh > 0) {
List<Cell> adjacent = new ArrayList<>();
for (Cell cell : frontier) {
for (int[] step : DIRECTIONS) {
Cell adjCell = getCellIfFresh(grid, cell.i + step[0], cell.j + step[1]);
if (adjCell == null) {
continue;
}
// mark immediately as rotten to avoid adding same fresh cell multiple times
// when it is adjacent to several frontier cells
grid[adjCell.i][adjCell.j] = ROTTEN;
countFresh--;
adjacent.add(adjCell);
}
}
if (adjacent.isEmpty()) {
// empty list implies rot spread as far as it could BUT countFresh is still > 0
// decrementing countFresh co-occurs with adding adjacent cells to the list
return -1;
}
countMinutes++;
frontier = adjacent;
}
return countMinutes;
}
private Cell getCellIfFresh(int[][] grid, int i, int j) {
boolean validIndices = i >= 0 && i < grid.length && j >= 0 && j < grid[i].length;
return validIndices && grid[i][j] == FRESH ? new Cell(i, j) : null;
}
record Cell(int i, int j) {
}
}