-
Notifications
You must be signed in to change notification settings - Fork 0
/
RottingOranges.java
78 lines (73 loc) · 2.13 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
// https://leetcode.com/problems/rotting-oranges/
// #bfs
class Solution {
private static class Coordinate {
int x;
int y;
Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
}
private static boolean checkValidCoordiate(int x, int y, int[][] grid, boolean[][] visited) {
if (x < 0 || x >= grid.length) return false;
if (y < 0 || y >= grid[0].length) return false;
if (visited[x][y]) return false;
if (grid[x][y] != 1) return false;
return true;
}
public int orangesRotting(int[][] grid) {
// creat queue to store rotten oranges
// each rotten oranges
// => get all the oranges that is going to rotten to store in queue
// => update minute count
// check whether a fresh orange is exists or not
ArrayList<Coordinate> list = new ArrayList<>();
int n = grid.length, m = grid[0].length;
boolean[][] visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// check current orange is rotten or not
if (grid[i][j] == 2) {
visited[i][j] = true;
list.add(new Coordinate(i, j));
}
}
}
Queue<ArrayList<Coordinate>> queue = new LinkedList<>();
queue.add(list);
// neighbor coornidate ranges
int[] dx = {-1, 0, 0, 1};
int[] dy = {0, -1, 1, 0};
int minuteCount = 0;
while (!queue.isEmpty()) {
list = queue.poll();
boolean countable = false;
ArrayList<Coordinate> newList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < 4; j++) {
int x = list.get(i).x + dx[j];
int y = list.get(i).y + dy[j];
if (checkValidCoordiate(x, y, grid, visited)) {
visited[x][y] = true;
countable = true;
newList.add(new Coordinate(x, y));
}
}
}
if (countable) {
minuteCount++;
queue.add(newList);
}
}
// check
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1 && !visited[i][j]) {
return -1;
}
}
}
return minuteCount;
}
}