994. Rotting Oranges Medium

@problem@discussion
#Array#Breadth-First Search#Matrix



1/**
2 * [994] Rotting Oranges
3 *
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 * Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
12 *  
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2019/02/16/oranges.png" style="width: 650px; height: 137px;" />
15 * Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
16 * Output: 4
17 * 
18 * Example 2:
19 * 
20 * Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
21 * Output: -1
22 * Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
23 * 
24 * Example 3:
25 * 
26 * Input: grid = [[0,2]]
27 * Output: 0
28 * Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	m == grid.length
34 * 	n == grid[i].length
35 * 	1 <= m, n <= 10
36 * 	grid[i][j] is 0, 1, or 2.
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/rotting-oranges/
42// discuss: https://leetcode.com/problems/rotting-oranges/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn oranges_rotting(grid: Vec<Vec<i32>>) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_994() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.