-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_area_of_island.rs
131 lines (115 loc) · 3.81 KB
/
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::collections::HashSet;
/// You are given an `m x n` binary matrix `grid`. An island is a group of
/// `1`'s (representing land) connected 4-directionally (horizontal or
/// vertical.) You may assume all four edges of the grid are surrounded by
/// water.
///
/// The area of an island is the number of cells with a value `1` in the
/// island.
///
/// Return the maximum area of an island in `grid`. If there is no island,
/// return `0`.
struct Solution;
impl Solution {
fn row_len(grid: &Vec<Vec<i32>>) -> usize {
grid.len()
}
fn col_len(grid: &Vec<Vec<i32>>) -> usize {
if Self::row_len(grid) > 0 {
grid[0].len()
} else {
0
}
}
fn is_land(grid: &Vec<Vec<i32>>, row: usize, col: usize) -> bool {
grid[row][col] == 1
}
pub fn max_area_of_island(grid: Vec<Vec<i32>>) -> i32 {
let mut result = 0;
let mut seen = HashSet::new();
for row in 0..Self::row_len(&grid) {
for col in 0..Self::col_len(&grid) {
let key = (row, col);
if Self::is_land(&grid, row, col) && !seen.contains(&key) {
seen.insert(key);
let area = Self::worker(&grid, &mut seen, row, col);
if area > result {
result = area;
}
}
}
}
result
}
fn is_valid_row(grid: &Vec<Vec<i32>>, row: i32) -> bool {
row >= 0 && row < Self::row_len(&grid) as i32
}
fn is_valid_col(grid: &Vec<Vec<i32>>, col: i32) -> bool {
col >= 0 && col < Self::col_len(&grid) as i32
}
fn worker(
grid: &Vec<Vec<i32>>,
seen: &mut HashSet<(usize, usize)>,
row: usize,
col: usize,
) -> i32 {
let mut result = 1;
let directions = vec!['N', 'S', 'E', 'W'];
for dir in &directions {
let mut neighbor_row = row as i32;
let mut neighbor_col = col as i32;
match dir {
'N' => {
neighbor_row -= 1;
}
'S' => {
neighbor_row += 1;
}
'E' => {
neighbor_col += 1;
}
'W' => {
neighbor_col -= 1;
}
_ => {}
}
let valid_row = Self::is_valid_row(grid, neighbor_row);
let valid_col = Self::is_valid_col(grid, neighbor_col);
if valid_row && valid_col {
let neighbor_row = neighbor_row as usize;
let neighbor_col = neighbor_col as usize;
let key = (neighbor_row, neighbor_col);
if Self::is_land(grid, neighbor_row, neighbor_col) && !seen.contains(&key) {
seen.insert(key);
result += Self::worker(grid, seen, neighbor_row, neighbor_col);
}
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let grid = vec![
vec![0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
vec![0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
vec![0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
vec![0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
vec![0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
vec![0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
vec![0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
];
let result = Solution::max_area_of_island(grid);
assert_eq!(result, 6);
}
#[test]
fn example_2() {
let grid = vec![vec![0, 0, 0, 0, 0, 0, 0, 0]];
let result = Solution::max_area_of_island(grid);
assert_eq!(result, 0);
}
}