Skip to content

Commit c883c7f

Browse files
committed
feat(graphs): add 695_max_area_of_island.rs
1 parent 60f8dd0 commit c883c7f

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,4 @@ so that you can use `just tf TEST` command to test.
152152
| Problem | Difficulty | Solution |
153153
| - | - | - |
154154
| 200. Number of Islands | Medium | [200_number_of_islands.rs](./tests/200_number_of_islands.rs) |
155+
| 695. Max Area of Island | Medium | [695_max_area_of_island.rs](./tests/695_max_area_of_island.rs) |

tests/695_max_area_of_island.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// 695. Max Area of Island
2+
// https://leetcode.com/problems/max-area-of-island/description/
3+
// Topics: Graphs.
4+
// Difficulty: Medium.
5+
6+
#[test]
7+
fn test_() {}
8+
9+
#[derive(Debug)]
10+
pub struct Solution;
11+
12+
// ---------------------------------
13+
// copy to leetcode starts from here
14+
// ---------------------------------
15+
16+
impl Solution {
17+
pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
18+
let mut max_area: i32 = 0;
19+
for x in 0..grid.len() {
20+
for y in 0..grid[0].len() {
21+
if grid[x][y] == 1 {
22+
max_area = max_area.max(find(&mut grid, x, y));
23+
}
24+
}
25+
}
26+
max_area
27+
}
28+
}
29+
30+
fn find(grid: &mut [Vec<i32>], x: usize, y: usize) -> i32 {
31+
if x >= grid.len() || y >= grid[0].len() {
32+
return 0;
33+
}
34+
if grid[x][y] != 1 {
35+
return 0;
36+
}
37+
grid[x][y] = 0;
38+
39+
let v1 = find(grid, x + 1, y);
40+
let v2 = find(grid, x, y + 1);
41+
let v3 = find(grid, x.wrapping_sub(1), y);
42+
let v4 = find(grid, x, y.wrapping_sub(1));
43+
44+
v1 + v2 + v3 + v4 + 1
45+
}

0 commit comments

Comments
 (0)