Skip to content

Commit e5b0486

Browse files
Problem 200 (#38)
* add initial solution * add bfs solution and resources * update readme
1 parent d9a1a6d commit e5b0486

File tree

2 files changed

+154
-1
lines changed

2 files changed

+154
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
4848
37. [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
4949
38. [Min Stack #155](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/min-stack-155.md)
5050
39. [Validate Binary Search Tree #98](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/validate-binary-tree-98.md)
51-
40. Number of Islands #200
51+
40. [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
5252
41. Rotting Oranges #994
5353
42. Search in Rotated Sorted Array #33
5454
43. Combination Sum #39
@@ -111,6 +111,7 @@ In order to practice with similar data structures I'll be placing each problem i
111111
- [Meeting Rooms II](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/meeting-rooms-ii-253.md)
112112
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
113113
- [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
114+
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
114115

115116
### Queue
116117

@@ -120,6 +121,7 @@ In order to practice with similar data structures I'll be placing each problem i
120121
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
121122
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
122123
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
124+
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
123125

124126
### Stack
125127

@@ -131,6 +133,7 @@ In order to practice with similar data structures I'll be placing each problem i
131133
- [Evaluate Reverse Polish Notation #150](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/polish-notation-150.md)
132134
- [Min Stack #155](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/min-stack-155.md)
133135
- [Validate Binary Search Tree #98](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/validate-binary-tree-98.md)
136+
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
134137

135138
### Linked Lists
136139

@@ -217,6 +220,7 @@ Within the problems above there are several patterns that often occur. I plan to
217220
- [Number of Provinces #547](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-provinces-547.md)
218221
- [Course Schedule #207](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/course-schedule-207.md)
219222
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
223+
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
220224

221225
### Depth First Search
222226

@@ -230,6 +234,7 @@ Within the problems above there are several patterns that often occur. I plan to
230234
- [Number of Provinces #547](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-provinces-547.md)
231235
- [Course Schedule #207](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/course-schedule-207.md)
232236
- [Validate Binary Search Tree #98](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/validate-binary-tree-98.md)
237+
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
233238

234239
### Divide & Conquer
235240

medium/number-of-islands-200.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Number of Islands
2+
3+
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+
const numIslands = function (grid) {
65+
const h = grid.length;
66+
const w = 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+
function dfs(i, j) {
83+
// Check for in bounds
84+
const bounds = 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+
const numIslands = function (grid) {
104+
const h = grid.length;
105+
const w = grid[0].length;
106+
const visited = new Set();
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+
function bfs(i, j) {
123+
const queue = [];
124+
visited.add(`${i}, ${j}`);
125+
queue.push([i, j]);
126+
127+
while (queue.length > 0) {
128+
const [i, j] = queue.shift();
129+
const dir = [
130+
[0, 1],
131+
[1, 0],
132+
[0, -1],
133+
[-1, 0],
134+
];
135+
for (let d of dir) {
136+
const row = i + d[0];
137+
const col = j + d[1];
138+
// Check for in bounds
139+
const bounds = row >= 0 && row < h && col >= 0 && col < w;
140+
if (bounds && !visited.has(`${row}, ${col}`) && grid[row][col] === '1') {
141+
visited.add(`${row}, ${col}`);
142+
queue.push([row, col]);
143+
}
144+
}
145+
}
146+
}
147+
};
148+
```

0 commit comments

Comments
 (0)