forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0695.js
More file actions
27 lines (25 loc) · 725 Bytes
/
Copy path0695.js
File metadata and controls
27 lines (25 loc) · 725 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
var maxAreaOfIsland = function(grid) {
let r = grid.length, c = grid[0].length;
let d = [0, 1, 0, -1, 0];
let res = 0;
let dfs = function(grid, x, y) {
let res = 1;
for (let i = 0; i < 4; i++) {
let nx = x + d[i], ny = y + d[i + 1];
if (0 <= nx && nx < r && 0 <= ny && ny < c && grid[nx][ny] == 1) {
grid[nx][ny] = 0;
res += dfs(grid, nx, ny);
}
}
return res;
}
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++) {
if (grid[i][j] == 1) {
grid[i][j] = 0;
res = Math.max(res, dfs(grid, i, j));
}
}
}
return res;
};