-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-islands.js
53 lines (37 loc) · 1.28 KB
/
number-of-islands.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function(grid) {
const gridLength = grid.length
const rowLength = grid[0].length
let count = 0
// Loop through every cell in the grid
for(let i = 0; i < gridLength; i++){
for(let j = 0; j < rowLength; j++) {
// When we hit land we know we are on an island
// We will use this cell to visit all the 1's connected to it
// Once we've visited all the 1's connected to this cell then we increase the count
if(grid[i][j]==="1") {
dfs(i, j)
count++
}
}
}
// DFS search
// Look in all directions and set the current cell to 0 to prevent this cell from being visited again
function dfs(i,j) {
if(grid[i][j] ==="1") {
grid[i][j] = '0'
// look north
if(i - 1 >= 0) dfs(i-1,j)
// look east
if(j + 1 < rowLength) dfs(i,j+1)
// look south
if(i + 1 < gridLength) dfs(i+1,j)
// look west
if(j - 1 >= 0) dfs(i,j-1)
}
}
return count
};