-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotting_oranges.rs
108 lines (96 loc) · 3.26 KB
/
rotting_oranges.rs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::collections::HashSet;
use std::collections::VecDeque;
/// You are given an `m x n` `grid` where each cell can have one of three
/// values:
///
/// * `0` representing an empty cell,
///
/// * `1` representing a fresh orange, or
///
/// * `2` representing a rotten orange.
///
/// Every minute any fresh orange that is 4-directionally adjacent to a rotten
/// orange becomes rotten.
///
/// Return the minimum number of minutes that must elapse until no cell has a
/// fresh orange. If this is impossible, return `-1`.
struct Solution;
impl Solution {
pub fn oranges_rotting(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut fresh = HashSet::new();
let mut seen = HashSet::new();
let mut queue = VecDeque::new();
for i in 0..m {
for j in 0..n {
let state = grid[i][j];
if state == 1 {
fresh.insert((i, j));
} else if state == 2 {
seen.insert((i, j));
queue.push_back((i, j));
}
}
}
if fresh.is_empty() { 0 }
else {
let directions = vec!['N', 'S', 'E', 'W'];
let mut result = 0;
while !queue.is_empty() && !fresh.is_empty() {
result += 1;
let q = queue.len();
for _ in 0..q {
let (i, j) = queue.pop_front().unwrap();
for dir in &directions {
let mut row = i as i32;
let mut col = j as i32;
match dir {
'N' => { row -= 1; }
'S' => { row += 1; }
'E' => { col += 1; }
'W' => { col -= 1; }
_ => { }
}
let valid_row = row >= 0 && row < m as i32;
let valid_col = col >= 0 && col < n as i32;
if valid_row && valid_col {
let row = row as usize;
let col = col as usize;
if !seen.contains(&(row, col)) {
if grid[row][col] == 1 {
seen.insert((row, col));
fresh.remove(&(row, col));
queue.push_back((row, col));
}
}
}
}
}
}
if fresh.is_empty() { result } else { -1 }
}
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let grid = vec![vec![2,1,1], vec![1,1,0], vec![0,1,1]];
let result = Solution::oranges_rotting(grid);
assert_eq!(result, 4);
}
#[test]
fn example_2() {
let grid = vec![vec![2,1,1], vec![0,1,1], vec![1,0,1]];
let result = Solution::oranges_rotting(grid);
assert_eq!(result, -1);
}
#[test]
fn example_3() {
let grid = vec![vec![0,2]];
let result = Solution::oranges_rotting(grid);
assert_eq!(result, 0);
}
}