Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9599a1c

Browse files
solved: 695. Max Area of Island
1 parent 77e1e3f commit 9599a1c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn max_area_of_island(grid: Vec<Vec<i32>>) -> i32 {
3+
let mut grid = grid;
4+
let (rows, cols) = (grid.len(), grid[0].len());
5+
6+
let mut max_area = 0;
7+
8+
for row in 0..rows {
9+
for col in 0..cols {
10+
max_area = max_area.max(Self::dfs(&mut grid, row, col))
11+
}
12+
}
13+
14+
max_area
15+
}
16+
17+
fn dfs(grid: &mut Vec<Vec<i32>>, row: usize, col: usize) -> i32 {
18+
let (rows, cols) = (grid.len(), grid[0].len());
19+
if row >= rows || col >= cols || grid[row][col] == 0 {
20+
return 0;
21+
}
22+
23+
grid[row][col] = 0;
24+
25+
1 + Self::dfs(grid, row + 1, col)
26+
+ Self::dfs(grid, row, col + 1)
27+
+ Self::dfs(grid, row - 1, col)
28+
+ Self::dfs(grid, row, col - 1)
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /