-
Notifications
You must be signed in to change notification settings - Fork 0
/
s0695_max_area_of_island.rs
53 lines (49 loc) · 1.59 KB
/
s0695_max_area_of_island.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
struct Solution;
//leetcode submit region begin(Prohibit modification and deletion)
impl Solution {
pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
let mut count = 0;
for i in 0..grid.len() {
for j in 0..grid[0].len() {
count = count.max(Self::dfs(&mut grid, i as i32, j as i32));
}
}
count
}
pub fn dfs(grid: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {
let ui = i as usize;
let uj = j as usize;
if i < 0 || ui >= grid.len() || j < 0 || uj >= grid[0].len() || grid[ui][uj] == 0 {
return 0;
}
grid[ui][uj] = 0;
let mut count = 1;
count += Self::dfs(grid, i - 1, j);
count += Self::dfs(grid, i, j - 1);
count += Self::dfs(grid, i + 1, j);
count += Self::dfs(grid, i, j + 1);
count
}
}
//leetcode submit region end(Prohibit modification and deletion)
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let grid: Vec<Vec<i32>> = [
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
]
.iter()
.map(|v| v.to_vec())
.collect();
assert_eq!(Solution::max_area_of_island(grid), 6);
}
}