-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsolution1.js
More file actions
34 lines (33 loc) · 757 Bytes
/
solution1.js
File metadata and controls
34 lines (33 loc) · 757 Bytes
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
/**
* https://leetcode.com/problems/max-area-of-island/
*
*
* 695. Max Area of Island
*
* Medium
*
* 88ms 59.66%
* 37mb 52.85%
*/
const maxAreaOfIsland = grid => {
let ans = 0
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
const item = grid[i][j]
if (item === 0) {
continue
}
ans = Math.max(bfs(grid, i, j), ans)
}
}
return ans
}
function bfs(grid, curRow, curCol) {
if (!grid[curRow] || !grid[curRow][curCol] || grid[curRow][curCol] === 0) {
return 0
}
let count = 1
grid[curRow][curCol] = 0
count += dfs(grid, curRow, curCol - 1) + dfs(grid, curRow, curCol + 1) + dfs(grid, curRow - 1, curCol) + dfs(grid, curRow + 1, curCol)
return count
}