You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Page on leetcode: https://leetcode.com/problems/number-of-islands/
4
+
5
+
## Problem Statement
6
+
7
+
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
8
+
9
+
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
10
+
11
+
### Constraints
12
+
13
+
- m == grid.length
14
+
- n == grid[i].length
15
+
- 1 <= m, n <= 300
16
+
- grid[i][j] is '0' or '1'.
17
+
18
+
### Example
19
+
20
+
```
21
+
Input: grid = [
22
+
["1","1","1","1","0"],
23
+
["1","1","0","1","0"],
24
+
["1","1","0","0","0"],
25
+
["0","0","0","0","0"]
26
+
]
27
+
Output: 1
28
+
```
29
+
30
+
```
31
+
Input: grid = [
32
+
["1","1","0","0","0"],
33
+
["1","1","0","0","0"],
34
+
["0","0","1","0","0"],
35
+
["0","0","0","1","1"]
36
+
]
37
+
Output: 3
38
+
```
39
+
40
+
## Solution
41
+
42
+
- Only 1's that are horizontal/vertical connected count, no diagonals
43
+
- Need to store nodes that have been visited, maybe a set
44
+
- DFS or BFS search in four directions
45
+
46
+
### Pseudocode
47
+
48
+
1. Create set
49
+
2. Create counter for islands
50
+
3. Iterate thru matrix nest i j loops
51
+
4. If cell is 1 and isn't in set call dfs function
52
+
5. number of islands++
53
+
6. create dfs recursive helper
54
+
7. if cell is 1 and in bounds and not in set
55
+
8. then add to set
56
+
9. recursive calls on all four directions
57
+
10. return num of islands
58
+
59
+
### Initial Solution
60
+
61
+
This solution has a time and space complexity O(mn).
62
+
63
+
```javascript
64
+
constnumIslands=function (grid) {
65
+
consth=grid.length;
66
+
constw= grid[0].length;
67
+
68
+
let totalIslands =0;
69
+
70
+
// Iterate through the matrix
71
+
for (let i =0; i < h; i++) {
72
+
for (let j =0; j < w; j++) {
73
+
if (grid[i][j] ==='1') {
74
+
dfs(i, j);
75
+
totalIslands++;
76
+
}
77
+
}
78
+
}
79
+
return totalIslands;
80
+
81
+
// DFS helper function
82
+
functiondfs(i, j) {
83
+
// Check for in bounds
84
+
constbounds= i >=0&& i < h && j >=0&& j < w;
85
+
86
+
// If valid cell add to set and start recursion
87
+
if (bounds && grid[i][j] ==='1') {
88
+
grid[i][j] ='2';
89
+
dfs(i +1, j);
90
+
dfs(i, j +1);
91
+
dfs(i -1, j);
92
+
dfs(i, j -1);
93
+
}
94
+
}
95
+
};
96
+
```
97
+
98
+
### Optimized Solution
99
+
100
+
The following solution has the same time and space complexity, but is approach with iterative BFS instead of recursive DFS. You can see and explanation here: https://www.youtube.com/watch?v=pV2kpPD66nE
101
+
102
+
```javascript
103
+
constnumIslands=function (grid) {
104
+
consth=grid.length;
105
+
constw= grid[0].length;
106
+
constvisited=newSet();
107
+
let totalIslands =0;
108
+
109
+
// Iterate through the matrix
110
+
for (let i =0; i < h; i++) {
111
+
for (let j =0; j < w; j++) {
112
+
if (grid[i][j] ==='1'&&!visited.has(`${i}, ${j}`)) {
113
+
bfs(i, j);
114
+
totalIslands++;
115
+
}
116
+
}
117
+
}
118
+
119
+
return totalIslands;
120
+
121
+
// BFS helper function
122
+
functionbfs(i, j) {
123
+
constqueue= [];
124
+
visited.add(`${i}, ${j}`);
125
+
queue.push([i, j]);
126
+
127
+
while (queue.length>0) {
128
+
const [i, j] =queue.shift();
129
+
constdir= [
130
+
[0, 1],
131
+
[1, 0],
132
+
[0, -1],
133
+
[-1, 0],
134
+
];
135
+
for (let d of dir) {
136
+
constrow= i + d[0];
137
+
constcol= j + d[1];
138
+
// Check for in bounds
139
+
constbounds= row >=0&& row < h && col >=0&& col < w;
140
+
if (bounds &&!visited.has(`${row}, ${col}`) && grid[row][col] ==='1') {
0 commit comments