(grid)
| 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 |
| 3 | function 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 | |
| 17 | function search(grid, row, col) { |
| 18 | const stack = [[row, col]]; |
no test coverage detected