Skip to content

Commit acb3674

Browse files
committed
add 695 script.
1 parent 6408f2c commit acb3674

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

695-max-area-of-island.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
const maxAreaOfIsland = function(grid) {
6+
let res = 0
7+
const seen = []
8+
for(let i = 0; i < grid.length; i++) {
9+
seen[i] = []
10+
}
11+
for(let i = 0; i < grid.length; i++) {
12+
for(let j = 0; j < grid[0].length; j++) {
13+
res = Math.max(res, area(i, j, seen, grid))
14+
}
15+
}
16+
return res
17+
};
18+
19+
function area(r, c, seen, grid) {
20+
console.log(grid)
21+
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || seen[r][c] || grid[r][c] == 0) return 0;
22+
seen[r][c] = true;
23+
return (1 + area(r+1, c, seen, grid) + area(r-1, c, seen, grid) + area(r, c-1, seen, grid) + area(r, c+1, seen, grid));
24+
}
25+
26+
console.log(maxAreaOfIsland([[1,1,0,0,0],[1,1,0,0,0],[0,0,0,1,1],[0,0,0,1,1]]))
27+
console.log(maxAreaOfIsland([[1,0],[1,1]]))
28+
console.log(maxAreaOfIsland([[1,1],[1,0]]))

0 commit comments

Comments
 (0)