-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-area-of-island.js
36 lines (33 loc) · 958 Bytes
/
max-area-of-island.js
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
35
36
/**
* @param {number[][]} grid
* @return {number}
*/
var maxAreaOfIsland = function(grid) {
let answer = 0;
let seen = newBiArray(grid.length, grid[0].length);
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
answer = Math.max(answer, area(i, j, grid, seen));
}
}
return answer;
};
var newBiArray = function(r, c){
var gfg = new Array(r);
for (var i = 0; i < gfg.length; i++) {
gfg[i] = new Array(c);
}
return gfg;
}
/**
* @param {number} r
* @param {number} c
* @param {number[][]} grid
* @param {boolean[][]} seen
* @return {number}
*/
var area = function(r, c, grid, seen){
if(r < 0 || c < 0 || r == grid.length || c == grid[0].length || grid[r][c] == 0 || seen[r][c]) return 0;
seen[r][c] = true;
return 1 + area(r, c-1, grid, seen) + area(r-1, c, grid, seen) + area(r, c+1, grid, seen) + area(r+1, c, grid, seen);
}