Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f732c09

Browse files
committed
Add description.md and solution.md to NumberOfIslands
1 parent 3549f2a commit f732c09

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
### Approach
2+
3+
Perform a linear scan of the 2D grid map. If a node contains a '1', it is considered a root node that initiates a Breadth First Search. Add this node to a queue and change its value to '0' to indicate that it has been visited. Continue to search the neighboring nodes of the nodes in the queue iteratively until the queue is empty.
4+
5+
### Implementation
6+
7+
```cpp
8+
class Solution {
9+
void sinkIsland(vector<vector<char>>& grid, int startI, int startJ) {
10+
deque<pair<int, int>> pairs;
11+
pairs.push_back(make_pair(startI, startJ));
12+
13+
const auto tryAdd = [&grid, &pairs] (int i, int j) {
14+
if (i < 0 || j < 0) return;
15+
if (i >= grid.size() || j >= grid.front().size()) return;
16+
17+
pairs.push_back({i, j});
18+
};
19+
20+
while (!pairs.empty()) {
21+
const auto [i, j] = pairs.front();
22+
pairs.pop_front();
23+
24+
if (grid[i][j] == '0') continue;
25+
grid[i][j] = '0';
26+
27+
tryAdd(i - 1, j);
28+
tryAdd(i + 1, j);
29+
tryAdd(i, j - 1);
30+
tryAdd(i, j + 1);
31+
}
32+
}
33+
34+
public:
35+
int numIslands(vector<vector<char>>& grid) {
36+
int nIslands = 0;
37+
for (int i = 0; i < grid.size(); ++i) {
38+
for (int j = 0; j < grid.front().size(); ++j) {
39+
if (grid[i][j] == '1') {
40+
++nIslands;
41+
sinkIsland(grid, i, j);
42+
}
43+
}
44+
}
45+
return nIslands;
46+
}
47+
};
48+
```
49+
50+
### Complexity Analysis
51+
52+
* Time Complexity: The time complexity is **O(M*N)**, where **M** represents the number of rows and **N** represents the number of columns.
53+
54+
* Space Complexity: The space complexity is **O(M*N)**.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Number of Islands
2+
3+
Given a binary grid of size ```m x n``` that represents a map, where ```'1'``` signifies land and ```'0'``` signifies water, calculate and return the total number of islands present.
4+
5+
An island is surrounded by water and is formed by connecting neightbouring lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
6+
<br>
7+
<br>
8+
<br>
9+
***Example 1:***
10+
11+
&emsp;&emsp;***Input:*** grid = [<br>
12+
&emsp;&emsp;&emsp;&emsp;["1","1","1","1","0"],<br>
13+
&emsp;&emsp;&emsp;&emsp;["1","1","0","1","0"],<br>
14+
&emsp;&emsp;&emsp;&emsp;["1","1","0","0","0"],<br>
15+
&emsp;&emsp;&emsp;&emsp;["0","0","0","0","0"]<br>
16+
&emsp;&emsp;]<br>
17+
&emsp;&emsp;***Output:*** 1
18+
19+
***Example 2:***
20+
21+
&emsp;&emsp;***Input:*** grid = [<br>
22+
&emsp;&emsp;&emsp;&emsp;["1","1","0","0","0"],<br>
23+
&emsp;&emsp;&emsp;&emsp;["1","1","0","0","0"],<br>
24+
&emsp;&emsp;&emsp;&emsp;["0","0","1","0","0"],<br>
25+
&emsp;&emsp;&emsp;&emsp;["0","0","0","1","1"]<br>
26+
&emsp;&emsp;]<br>
27+
&emsp;&emsp;***Output:*** 3
28+
29+
30+
***Constraints:***
31+
32+
* ``m == grid.length``
33+
* ``n == grid[i].length``
34+
* ``1 <= m, n <= 300``
35+
* ``grid[i][j] is '0' or '1'``

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /