-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrotten.java
68 lines (55 loc) · 2.31 KB
/
rotten.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
import java.util.LinkedList;
import java.util.Queue;
class RottenOranges {
// Helper class to represent coordinates in the grid
static class Orange {
int row, col, time;
public Orange(int row, int col, int time) {
this.row = row;
this.col = col;
this.time = time;
}
} //s dsf
public static int orangesRotting(int[][] grid) {
if (grid == null || grid.length == 0) return -1;
int rows = grid.length;
int cols = grid[0].length;
Queue<Orange> queue = new LinkedList<>();
int freshOranges = 0;
int timeElapsed = 0;
// Initialize the queue with all rotten oranges and count fresh oranges
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 2) {
queue.add(new Orange(i, j, 0)); // Rotten oranges with initial time 0
} else if (grid[i][j] == 1) {
freshOranges++;
}
}
}
// Directions for adjacent cells (up, down, left, right)
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
// Perform BFS
while (!queue.isEmpty()) {
Orange current = queue.poll();
int row = current.row;
int col = current.col;
int time = current.time;
// Update the time elapsed
timeElapsed = time;
// Spread the rot to adjacent fresh oranges
for (int[] direction : directions) {
int newRow = row + direction[0];
int newCol = col + direction[1];
// Check if the adjacent cell is within bounds and contains a fresh orange
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == 1) {
grid[newRow][newCol] = 2; // The fresh orange becomes rotten
freshOranges--; // Decrease the count of fresh oranges
queue.add(new Orange(newRow, newCol, time + 1)); // Add the new rotten orange to the queue
}
}
}
// If there are no fresh oranges left, return the time, otherwise return -1
return freshOranges == 0 ? timeElapsed : -1;
}
public static void main