MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / numIslands

Function numIslands

matrix/number-of-islands.js:3–15  ·  view source on GitHub ↗
(grid)

Source from the content-addressed store, hash-verified

1// time O(wh) where w is the width of the grid and h the height
2// space O(n) where n is the number of items in our stack search
3function numIslands(grid) {
4 let numIslands = 0;
5
6 for (let row = 0; row < grid.length; row++) {
7 for (let col = 0; col < grid[row].length; col++) {
8 if (grid[row][col] === "1") {
9 search(grid, row, col);
10 numIslands++;
11 }
12 }
13 }
14 return numIslands;
15}
16
17function search(grid, row, col) {
18 const stack = [[row, col]];

Callers 1

Calls 1

searchFunction · 0.85

Tested by

no test coverage detected