Skip to content

Commit 3f931d5

Browse files
authored
Merge pull request #862 from 0xff-dev/994
Add solution and test-cases for problem 994
2 parents 74896c5 + 6461d33 commit 3f931d5

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

leetcode/901-1000/0994.Rotting-Oranges/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [994.Rotting Oranges][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an `m x n` grid where each cell can have one of three values:
5+
6+
- `0` representing an empty cell,
7+
- `1` representing a fresh orange, or
8+
- `2` representing a rotten orange.
9+
10+
Every minute, any fresh orange that is **4-directionally adjacent** to a rotten orange becomes rotten.
11+
12+
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return `-1`.
13+
14+
**Example 1:**
715

8-
**Example 1:**
16+
![1](./oranges.png)
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
20+
Output: 4
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Rotting Oranges
23-
```go
2425
```
26+
Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
27+
Output: -1
28+
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
29+
```
30+
31+
**Example 3:**
2532

33+
```
34+
Input: grid = [[0,2]]
35+
Output: 0
36+
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
37+
```
2638

2739
## 结语
2840

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) int {
4+
used := map[[2]int]struct{}{}
5+
rows, cols := len(grid), len(grid[0])
6+
freshOranges := 0
7+
queue := [][2]int{}
8+
for r := 0; r < rows; r++ {
9+
for c := 0; c < cols; c++ {
10+
if grid[r][c] == 2 {
11+
queue = append(queue, [2]int{r, c})
12+
used[[2]int{r, c}] = struct{}{}
13+
}
14+
if grid[r][c] == 1 {
15+
freshOranges++
16+
}
17+
}
18+
}
19+
var dirs = [][2]int{
20+
{-1, 0}, {1, 0}, {0, 1}, {0, -1},
21+
}
22+
steps := 0
23+
for len(queue) > 0 {
24+
nq := make([][2]int, 0)
25+
for _, cur := range queue {
26+
for _, dir := range dirs {
27+
nx, ny := cur[0]+dir[0], cur[1]+dir[1]
28+
if nx >= 0 && nx < rows && ny >= 0 && ny < cols && grid[nx][ny] == 1 {
29+
key := [2]int{nx, ny}
30+
if _, ok := used[key]; !ok {
31+
nq = append(nq, key)
32+
used[key] = struct{}{}
33+
}
34+
}
35+
}
36+
}
37+
freshOranges -= len(nq)
38+
if len(nq) != 0 {
39+
steps++
40+
}
41+
queue = nq
42+
}
43+
if freshOranges > 0 {
44+
return -1
45+
}
46+
return steps
547
}

leetcode/901-1000/0994.Rotting-Oranges/Solution_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{
17+
{2, 1, 1}, {1, 1, 0}, {0, 1, 1},
18+
}, 4},
19+
{"TestCase2", [][]int{
20+
{2, 1, 1}, {0, 1, 1}, {1, 0, 1},
21+
}, -1},
22+
{"TestCase3", [][]int{{0, 2}}, 0},
1923
}
2024

2125
// 开始测试
@@ -30,10 +34,10 @@ func TestSolution(t *testing.T) {
3034
}
3135
}
3236

33-
// 压力测试
37+
// 压力测试
3438
func BenchmarkSolution(b *testing.B) {
3539
}
3640

37-
// 使用案列
41+
// 使用案列
3842
func ExampleSolution() {
3943
}
Loading

0 commit comments

Comments
 (0)