Skip to content

Commit d88ad53

Browse files
committed
[LEET] 695 Max Area of Island (Medium)
1 parent db7d425 commit d88ad53

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

이용훈/8주차/260216.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
var maxAreaOfIsland = function(grid) {
6+
const m = grid.length;
7+
const n = grid[0].length;
8+
9+
const dx = [-1, 0, 1, 0];
10+
const dy = [0, 1, 0, -1];
11+
12+
const dfs = (x, y) => {
13+
if (x < 0 || x >= m || y < 0 || y >= n) return 0;
14+
if (grid[x][y] === 0) return 0;
15+
16+
grid[x][y] = 0;
17+
18+
let cnt = 1;
19+
for(let i = 0; i < 4; i++) {
20+
const nx = x + dx[i];
21+
const ny = y + dy[i];
22+
23+
cnt += dfs(nx, ny);
24+
}
25+
26+
return cnt;
27+
}
28+
29+
let max = 0;
30+
for(let i = 0; i < m; i++) {
31+
for(let j = 0; j < n; j++) {
32+
if(grid[i][j] === 1){
33+
max = Math.max(max, dfs(i, j));
34+
}
35+
}
36+
}
37+
38+
return max;
39+
};

0 commit comments

Comments
 (0)