994. Rotting Oranges
Last updated
Last updated
https://leetcode.com/problems/rotting-oranges/
ๆ่ทฏ่ท 1162 ๅทฎไธๅค, ๅชๆฏ่ฆๆณจๆไธไบๆขไปถ
ๅ fresh ็ๆฉๅญ็ๅๆธๆๅฝฑ้ฟๅฐ็ตๆ,
้ๆ่ฆ็ๆธ ๆฅ ้ก็ฎ็ โไพๅญโ
ex2้ๆจฃๆๆฏ -1, ex3 ๆๆฏ 0
O(nm), O(nm)
class Solution {
int dirs[][] = {{0,1}, {0,-1}, {1,0}, {-1, 0}};
public int orangesRotting(int[][] grid) {
int m = grid.length; //height
if (m == 0) {
return -1;
}
int n = grid[0].length; //width
int freshCount = 0;
Queue<int[]> q = new ArrayDeque<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 2) {
q.offer(new int[]{i, j}); //rotten (x,y) add to q first
} else if (grid[i][j] == 1) { // count fresh
freshCount++;
}
}
}
if (freshCount == 0) { //ex3็ๆ
ๆณ
return 0;
}
int count = -1;
while (!q.isEmpty()) {
count++;
int levels = q.size();
for (int i = 0; i < levels; i++) {
int rotten[] = q.poll();
int r = rotten[0];
int c = rotten[1];
for (int d[] : dirs) {
int r0 = r + d[0];
int c0 = c + d[1];
if (inArea(r0, c0, grid) && grid[r0][c0] == 1) {
grid[r0][c0] = 2;
q.offer(new int[]{r0, c0}); // ๅ ๅ
ฅๆฐ็ rotten
freshCount--;
}
}
}
}
return freshCount == 0 ? count : -1; // -1: ex2็ๆ
ๆณ
}
private boolean inArea(int r, int c, int[][] grid) {
return (r >= 0 && r < grid.length && c >=0 && c < grid[0].length);
}
}
/*
0 - no oranges
1 - fresh
2 - rotten
use BFS,
add rotten to queue
count fresh one's count
if a cell has rotten, make it's neightbor cell (if it's fresh) become rotten (like spread disease), fresh--
*/
class Solution {
private static final int DIRECTIONS[][] = {{1,0}, {0,1}, {-1,0}, {0,-1}};
public int orangesRotting(int[][] grid) {
Queue<int[]> queue = new LinkedList<>();
int freshCount = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 2) {
queue.add(new int[]{i,j});
}
if (grid[i][j] == 1) {
freshCount++;
}
}
}
if (freshCount == 0) return 0;
int result = -1; // why?
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
int cell[] = queue.poll();
int x = cell[0];
int y = cell[1];
for (int[] direction : DIRECTIONS) {
int newX = x + direction[0];
int newY = y + direction[1];
if (inArea(grid, newX, newY) && grid[newX][newY] == 1) {
grid[newX][newY] = 2; // makes it rotten
queue.add(new int[]{newX, newY});
freshCount--;
}
}
}
result++;
}
return freshCount == 0 ? result : -1;
}
private boolean inArea(int grid[][], int x, int y) {
return (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length);
}
}