Skip to content

Commit 6641677

Browse files
committed
feat: add solutions to lc problem: No.0994
No.0994.Rotting Oranges
1 parent d79ba26 commit 6641677

File tree

8 files changed

+332
-323
lines changed

8 files changed

+332
-323
lines changed

solution/0900-0999/0994.Rotting Oranges/README.md

+110-92
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,27 @@
6565
```python
6666
class Solution:
6767
def orangesRotting(self, grid: List[List[int]]) -> int:
68-
R, C = len(grid), len(grid[0])
69-
70-
# queue - all starting cells with rotting oranges
71-
queue = collections.deque()
72-
for r, row in enumerate(grid):
73-
for c, val in enumerate(row):
74-
if val == 2:
75-
queue.append((r, c, 0))
76-
77-
def neighbors(r, c) -> (int, int):
78-
for nr, nc in ((r - 1, c), (r, c - 1), (r + 1, c), (r, c + 1)):
79-
if 0 <= nr < R and 0 <= nc < C:
80-
yield nr, nc
81-
82-
d = 0
83-
while queue:
84-
r, c, d = queue.popleft()
85-
for nr, nc in neighbors(r, c):
86-
if grid[nr][nc] == 1:
87-
grid[nr][nc] = 2
88-
queue.append((nr, nc, d + 1))
89-
90-
if any(1 in row for row in grid):
91-
return -1
92-
return d
68+
m, n = len(grid), len(grid[0])
69+
q = deque()
70+
cnt = 0
71+
for i in range(m):
72+
for j in range(n):
73+
if grid[i][j] == 2:
74+
q.append((i, j))
75+
elif grid[i][j] == 1:
76+
cnt += 1
77+
ans = 0
78+
while q and cnt:
79+
ans += 1
80+
for _ in range(len(q), 0, -1):
81+
i, j = q.popleft()
82+
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
83+
x, y = i + a, j + b
84+
if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:
85+
cnt -= 1
86+
grid[x][y] = 2
87+
q.append((x, y))
88+
return ans if cnt == 0 else -1
9389
```
9490

9591
### **Java**
@@ -98,46 +94,37 @@ class Solution:
9894

9995
```java
10096
class Solution {
101-
int[] dr = new int[] { -1, 0, 1, 0 };
102-
int[] dc = new int[] { 0, -1, 0, 1 };
103-
10497
public int orangesRotting(int[][] grid) {
105-
int R = grid.length, C = grid[0].length;
106-
Queue<Integer> queue = new ArrayDeque<Integer>();
107-
Map<Integer, Integer> depth = new HashMap<Integer, Integer>();
108-
for (int r = 0; r < R; ++r) {
109-
for (int c = 0; c < C; ++c) {
110-
if (grid[r][c] == 2) {
111-
int code = r * C + c;
112-
queue.add(code);
113-
depth.put(code, 0);
98+
int m = grid.length, n = grid[0].length;
99+
int cnt = 0;
100+
Deque<int[]> q = new LinkedList<>();
101+
for (int i = 0; i < m; ++i) {
102+
for (int j = 0; j < n; ++j) {
103+
if (grid[i][j] == 2) {
104+
q.offer(new int[]{i, j});
105+
} else if (grid[i][j] == 1) {
106+
++cnt;
114107
}
115108
}
116109
}
117110
int ans = 0;
118-
while (!queue.isEmpty()) {
119-
int code = queue.remove();
120-
int r = code / C, c = code % C;
121-
for (int k = 0; k < 4; ++k) {
122-
int nr = r + dr[k];
123-
int nc = c + dc[k];
124-
if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) {
125-
grid[nr][nc] = 2;
126-
int ncode = nr * C + nc;
127-
queue.add(ncode);
128-
depth.put(ncode, depth.get(code) + 1);
129-
ans = depth.get(ncode);
130-
}
131-
}
132-
}
133-
for (int[] row : grid) {
134-
for (int v : row) {
135-
if (v == 1) {
136-
return -1;
111+
int[] dirs = {1, 0, -1, 0, 1};
112+
while (!q.isEmpty() && cnt > 0) {
113+
++ans;
114+
for (int i = q.size(); i > 0; --i) {
115+
int[] p = q.poll();
116+
for (int j = 0; j < 4; ++j) {
117+
int x = p[0] + dirs[j];
118+
int y = p[1] + dirs[j + 1];
119+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
120+
grid[x][y] = 2;
121+
--cnt;
122+
q.offer(new int[]{x, y});
123+
}
137124
}
138125
}
139126
}
140-
return ans;
127+
return cnt > 0 ? -1 : ans;
141128
}
142129
}
143130
```
@@ -148,56 +135,87 @@ class Solution {
148135

149136
```cpp
150137
class Solution {
151-
int cnt;
152-
int dis[10][10];
153-
int dir_x[4] = {0, 1, 0, -1};
154-
int dir_y[4] = {1, 0, -1, 0};
155-
156138
public:
157-
int orangesRotting(vector<vector<int>> &grid) {
158-
queue<pair<int, int>> Q;
159-
memset(dis, -1, sizeof(dis));
160-
cnt = 0;
161-
int n = (int)grid.size(), m = (int)grid[0].size(), ans = 0;
162-
for (int i = 0; i < n; ++i)
139+
int orangesRotting(vector<vector<int>>& grid) {
140+
int m = grid.size(), n = grid[0].size();
141+
int cnt = 0;
142+
typedef pair<int, int> pii;
143+
queue<pii> q;
144+
for (int i = 0; i < m; ++i)
163145
{
164-
for (int j = 0; j < m; ++j)
146+
for (int j = 0; j < n; ++j)
165147
{
166-
if (grid[i][j] == 2)
167-
{
168-
Q.push(make_pair(i, j));
169-
dis[i][j] = 0;
170-
}
171-
else if (grid[i][j] == 1)
172-
cnt += 1;
148+
if (grid[i][j] == 2) q.emplace(i, j);
149+
else if (grid[i][j] == 1) ++cnt;
173150
}
174151
}
175-
while (!Q.empty())
152+
int ans = 0;
153+
vector<int> dirs = {-1, 0, 1, 0, -1};
154+
while (!q.empty() && cnt > 0)
176155
{
177-
pair<int, int> x = Q.front();
178-
Q.pop();
179-
for (int i = 0; i < 4; ++i)
156+
++ans;
157+
for (int i = q.size(); i > 0; --i)
180158
{
181-
int tx = x.first + dir_x[i];
182-
int ty = x.second + dir_y[i];
183-
if (tx < 0 || tx >= n || ty < 0 || ty >= m || ~dis[tx][ty] || !grid[tx][ty])
184-
continue;
185-
dis[tx][ty] = dis[x.first][x.second] + 1;
186-
Q.push(make_pair(tx, ty));
187-
if (grid[tx][ty] == 1)
159+
auto p = q.front();
160+
q.pop();
161+
for (int j = 0; j < 4; ++j)
188162
{
189-
cnt -= 1;
190-
ans = dis[tx][ty];
191-
if (!cnt)
192-
break;
163+
int x = p.first + dirs[j];
164+
int y = p.second + dirs[j + 1];
165+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1)
166+
{
167+
--cnt;
168+
grid[x][y] = 2;
169+
q.emplace(x, y);
170+
}
193171
}
194172
}
195173
}
196-
return cnt ? -1 : ans;
174+
return cnt > 0 ? -1 : ans;
197175
}
198176
};
199177
```
200178
179+
### **Go**
180+
181+
```go
182+
func orangesRotting(grid [][]int) int {
183+
m, n := len(grid), len(grid[0])
184+
cnt := 0
185+
var q [][]int
186+
for i := 0; i < m; i++ {
187+
for j := 0; j < n; j++ {
188+
if grid[i][j] == 2 {
189+
q = append(q, []int{i, j})
190+
} else if grid[i][j] == 1 {
191+
cnt++
192+
}
193+
}
194+
}
195+
ans := 0
196+
dirs := []int{-1, 0, 1, 0, -1}
197+
for len(q) > 0 && cnt > 0 {
198+
ans++
199+
for i := len(q); i > 0; i-- {
200+
p := q[0]
201+
q = q[1:]
202+
for j := 0; j < 4; j++ {
203+
x, y := p[0]+dirs[j], p[1]+dirs[j+1]
204+
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
205+
cnt--
206+
grid[x][y] = 2
207+
q = append(q, []int{x, y})
208+
}
209+
}
210+
}
211+
}
212+
if cnt > 0 {
213+
return -1
214+
}
215+
return ans
216+
}
217+
```
218+
201219
### **...**
202220

203221
```

0 commit comments

Comments
 (0)