-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberOfIslands.js
58 lines (57 loc) · 1.45 KB
/
numberOfIslands.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
54
55
56
57
58
var numIslands = function(grid) {
let matrix = [];
console.log(grid)
for(let itrR = 0 ; itrR < grid.length ; itrR++ ) {
let tempArr = [];
for(let itrC = 0; itrC < grid[itrR].length ; itrC++) {
tempArr.push(0);
}
matrix.push(tempArr);
}
let counter = 0;
for(let itrR = 0 ; itrR < grid.length ; itrR++ ) {
for(let itrC = 0; itrC < grid[itrR].length ; itrC++) {
if(grid[itrR][itrC] == "1" && !matrix[itrR][itrC]) {
dfs(itrR, itrC, grid, matrix);
counter++;
}
}
}
console.log(matrix)
console.log(counter)
return counter;
};
function dfs(x, y , grid, matrix) {
if(y < 0 || y >= grid[0].length || x < 0 || x >= grid.length) {
return;
}
console.log(x, y , grid, matrix)
if(grid[x][y] == "0" || matrix[x][y]) {
return;
}
matrix[x][y] = 1;
dfs(x - 1 , y , grid, matrix);
dfs(x , y + 1 , grid, matrix);
dfs(x + 1 , y , grid, matrix);
dfs(x , y - 1 , grid, matrix);
}
let dataSet = [
[
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
],
[
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
]
numIslands([
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
])