Skip to content

Commit 0255ceb

Browse files
authored
Add 695-Max Area of Island.js
1 parent 877408e commit 0255ceb

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

javascript/695-Max Area of Island.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var maxAreaOfIsland = function(grid) {
2+
function find(x, y) {
3+
if (grid[y] === undefined || grid[y][x] === undefined) {
4+
return 0;
5+
}
6+
7+
if (grid[y][x] === 0) {
8+
return 0;
9+
}
10+
11+
grid[y][x] = 0;
12+
13+
let square = 1;
14+
15+
square += find(x + 1, y);
16+
square += find(x - 1, y);
17+
square += find(x, y + 1);
18+
square += find(x, y - 1);
19+
20+
return square;
21+
}
22+
23+
let max = 0;
24+
25+
for (let y = 0; y < grid.length; y++) {
26+
for (let x = 0; x < grid[0].length; x++) {
27+
max = Math.max(max, find(x, y));
28+
}
29+
}
30+
31+
return max;
32+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy