Skip to content

Commit df03096

Browse files
committed
一刷994
1 parent ab44ad2 commit df03096

File tree

7 files changed

+141
-51
lines changed

7 files changed

+141
-51
lines changed

README.adoc

+8-8
Original file line numberDiff line numberDiff line change
@@ -6983,14 +6983,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
69836983
//|{doc_base_url}/0993-cousins-in-binary-tree.adoc[题解]
69846984
//|Easy
69856985
//|
6986-
//
6987-
//|{counter:codes}
6988-
//|{leetcode_base_url}/rotting-oranges/[994. Rotting Oranges^]
6989-
//|{source_base_url}/_0994_RottingOranges.java[Java]
6990-
//|{doc_base_url}/0994-rotting-oranges.adoc[题解]
6991-
//|Easy
6992-
//|
6993-
//
6986+
6987+
|{counter:codes}
6988+
|{leetcode_base_url}/rotting-oranges/[994. Rotting Oranges^]
6989+
|{source_base_url}/_0994_RottingOranges.java[Java]
6990+
|{doc_base_url}/0994-rotting-oranges.adoc[题解]
6991+
|Easy
6992+
|
6993+
69946994
//|{counter:codes}
69956995
//|{leetcode_base_url}/minimum-number-of-k-consecutive-bit-flips/[995. Minimum Number of K Consecutive Bit Flips^]
69966996
//|{source_base_url}/_0995_MinimumNumberOfKConsecutiveBitFlips.java[Java]

docs/0994-rotting-oranges.adoc

+56-42
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,84 @@
11
[#0994-rotting-oranges]
2-
= 994. Rotting Oranges
2+
= 994. 腐烂的橘子
33

4-
{leetcode}/problems/rotting-oranges/[LeetCode - Rotting Oranges^]
4+
https://leetcode.cn/problems/rotting-oranges/[LeetCode - 994. 腐烂的橘子 ^]
55

6-
In a given grid, each cell can have one of three values:
6+
在给定的 `m x n` 网格 `grid` 中,每个单元格可以有以下三个值之一:
77

8+
* 值 `0` 代表空单元格;
9+
* 值 `1` 代表新鲜橘子;
10+
* 值 `2` 代表腐烂的橘子。
811
9-
* the value `0` representing an empty cell;
10-
* the value `1` representing a fresh orange;
11-
* the value `2` representing a rotten orange.
12+
每分钟,腐烂的橘子 *周围 4 个方向上相邻* 的新鲜橘子都会腐烂。
1213

14+
返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 `-1`
1315

14-
Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.
16+
*示例 1:*
1517

16-
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return `-1` instead.
18+
image::images/0994-01.png[{image_attr}]
1719

18-
20+
....
21+
输入:grid = [[2,1,1],[1,1,0],[0,1,1]]
22+
输出:4
23+
....
1924

25+
*示例 2:*
2026

21-
*Example 1:*
27+
....
28+
输入:grid = [[2,1,1],[0,1,1],[1,0,1]]
29+
输出:-1
30+
解释:左下角的橘子(第 2 行, 第 0 列)永远不会腐烂,因为腐烂只会发生在 4 个方向上。
31+
....
2232

23-
image::https://assets.leetcode.com/uploads/2019/02/16/oranges.png[{image_attr}]
24-
25-
[subs="verbatim,quotes,macros"]
26-
----
27-
*Input:* [[2,1,1],[1,1,0],[0,1,1]]
28-
*Output:* 4
29-
----
30-
31-
32-
*Example 2:*
33-
34-
[subs="verbatim,quotes,macros"]
35-
----
36-
*Input:* [[2,1,1],[0,1,1],[1,0,1]]
37-
*Output:* -1
38-
*Explanation:* The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
39-
----
33+
*示例 3:*
4034

35+
....
36+
输入:grid = [[0,2]]
37+
输出:0
38+
解释:因为 0 分钟时已经没有新鲜橘子了,所以答案就是 0 。
39+
....
4140

42-
*Example 3:*
41+
*提示:*
4342

44-
[subs="verbatim,quotes,macros"]
45-
----
46-
*Input:* [[0,2]]
47-
*Output:* 0
48-
*Explanation:* Since there are already no fresh oranges at minute 0, the answer is just 0.
49-
----
50-
51-
52-
53-
*Note:*
43+
* `m == grid.length`
44+
* `n == grid[i].length`
45+
* `+1 <= m, n <= 10+`
46+
* `grid[i][j]` 仅为 `0``1``2`
5447
5548
56-
* `1 <= grid.length <= 10`
57-
* `1 <= grid[0].length <= 10`
58-
* `grid[i][j]` is only `0`, `1`, or `2`.
59-
49+
== 思路分析
6050

51+
对网格进行遍历,让橘子逐渐加深腐烂程度 `2` → `3` → `4` → ...,同时传染给相邻橘子,记录有被传染的橘子,如果被传染橘子数量不为0,则进行下一轮遍历。最后,再判断一下网格中是否还有新鲜橘子,如有,则返回 `-1`,没有则计算耗时。
6152

53+
看题解,记录腐烂橘子和新鲜橘子的位置,然后进行遍历的做法也不错。如下图:
6254

55+
image::images/0994-10.gif[{image_attr}]
6356

6457

6558
[[src-0994]]
59+
[tabs]
60+
====
61+
一刷::
62+
+
63+
--
6664
[{java_src_attr}]
6765
----
6866
include::{sourcedir}/_0994_RottingOranges.java[tag=answer]
6967
----
68+
--
69+
70+
// 二刷::
71+
// +
72+
// --
73+
// [{java_src_attr}]
74+
// ----
75+
// include::{sourcedir}/_0994_RottingOranges_2.java[tag=answer]
76+
// ----
77+
// --
78+
====
79+
80+
81+
== 参考资料
7082

83+
. https://leetcode.cn/problems/rotting-oranges/solutions/124765/fu-lan-de-ju-zi-by-leetcode-solution/[994. 腐烂的橘子 - 官方题解^]
84+
. https://leetcode.cn/problems/rotting-oranges/solutions/129542/yan-du-you-xian-sou-suo-python3-c-by-z1m/[994. 腐烂的橘子 - 广度优先搜索^]

docs/images/0994-01.png

41.6 KB
Loading

docs/images/0994-10.gif

14.6 MB
Loading

docs/index.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,7 @@ include::0992-subarrays-with-k-different-integers.adoc[leveloffset=+1]
20712071

20722072
// include::0993-cousins-in-binary-tree.adoc[leveloffset=+1]
20732073

2074-
// include::0994-rotting-oranges.adoc[leveloffset=+1]
2074+
include::0994-rotting-oranges.adoc[leveloffset=+1]
20752075

20762076
// include::0995-minimum-number-of-k-consecutive-bit-flips.adoc[leveloffset=+1]
20772077

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,11 @@ endif::[]
693693
|{doc_base_url}/1349-maximum-students-taking-exam.adoc[题解]
694694
|❌ 回溯解法通过 50 / 57 个测试用例。动态规划解法太难了!
695695

696+
|{counter:codes2503}
697+
|{leetcode_base_url}/rotting-oranges/[994. 腐烂的橘子^]
698+
|{doc_base_url}/0994-rotting-oranges.adoc[题解]
699+
|✅ 腐烂加剧+传染。广度优先遍历的解法也挺有意思。
700+
696701
|===
697702

698703
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0994_RottingOranges {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-15 22:20:47
9+
*/
10+
public int orangesRotting(int[][] grid) {
11+
int rotted = 2;
12+
while (rot(grid, rotted) > 0) {
13+
rotted++;
14+
}
15+
int m = grid.length;
16+
int n = grid[0].length;
17+
for (int i = 0; i < m; i++) {
18+
for (int j = 0; j < n; j++) {
19+
if (grid[i][j] == 1) {
20+
return -1;
21+
}
22+
}
23+
}
24+
return rotted - 2;
25+
}
26+
27+
private int rot(int[][] grid, int roted) {
28+
int m = grid.length;
29+
int n = grid[0].length;
30+
int count = 0;
31+
int next = roted + 1;
32+
for (int i = 0; i < m; i++) {
33+
for (int j = 0; j < n; j++) {
34+
if (grid[i][j] != roted) {
35+
continue;
36+
}
37+
grid[i][j] = next;
38+
// 上
39+
if (i > 0 && grid[i - 1][j] == 1) {
40+
grid[i - 1][j] = next;
41+
count++;
42+
}
43+
// 下
44+
if (i < m - 1 && grid[i + 1][j] == 1) {
45+
grid[i + 1][j] = next;
46+
count++;
47+
}
48+
// 左
49+
if (j > 0 && grid[i][j - 1] == 1) {
50+
grid[i][j - 1] = next;
51+
count++;
52+
}
53+
// 右
54+
if (j < n - 1 && grid[i][j + 1] == 1) {
55+
grid[i][j + 1] = next;
56+
count++;
57+
}
58+
}
59+
}
60+
return count;
61+
}
62+
63+
// end::answer[]
64+
public static void main(String[] args) {
65+
new _0994_RottingOranges().orangesRotting(new int[][]{
66+
{2, 1, 1},
67+
{1, 1, 0},
68+
{0, 1, 1}
69+
});
70+
}
71+
}

0 commit comments

Comments
 (0)