Skip to content

Commit 047d85b

Browse files
authored
Number of Islands question in C++ language. Pull Request for Issue #77 (#188)
* added number of islands question * Update number-of-islands.cpp * Update README.md * Update README.md * Update README.md
1 parent 56c1fa2 commit 047d85b

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

C++/number-of-islands.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public:
3+
bool visited[300][300];
4+
int r[4] = {-1,0,0,1};
5+
int c[4] = {0,-1,1,0};
6+
7+
//Checks if the given row and col value are valid and if the cell is visited and if the cell contains '1' or not.
8+
bool val(int row,int col,vector<vector<char>>& grid,int M,int N)
9+
{
10+
return (row<M && col<N && row>=0 && col>=0 && !visited[row][col] && grid[row][col]=='1');
11+
}
12+
13+
//Dfs function for exploring the surrounding cells
14+
void dfs(int i,int j,vector<vector<char>>& grid, int M, int N)
15+
{
16+
visited[i][j] = true;
17+
for(int a=0;a<4;a++)
18+
{
19+
int row = i + r[a];
20+
int col = j + c[a];
21+
if(val(row,col,grid,M,N))
22+
{
23+
dfs(row,col,grid,M,N);
24+
}
25+
}
26+
}
27+
28+
int numIslands(vector<vector<char>>& grid) {
29+
int m = grid.size();
30+
int n = grid[0].size();
31+
memset(visited,0,sizeof(visited));
32+
int island_count = 0;
33+
for(int i=0;i<m;i++)
34+
{
35+
for(int j=0;j<n;j++)
36+
{
37+
if(!visited[i][j] && grid[i][j]=='1')
38+
{
39+
dfs(i,j,grid,m,n);
40+
island_count++; // Island count is incremented when there is a cell that has not been visited and contains a '1'.
41+
//Dfs function marks the other connected '1's in the visited matrix.
42+
}
43+
}
44+
}
45+
return island_count;
46+
}
47+
};
48+
/* Time complexity : O(m x n) as each cell is accessed one time.
49+
Space complexity : O(m x n) as the visited matrix contains m x n elements.*/

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
347347
| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Java](./Java/path-sum.java) | _O(n)_ | _O(n)_ | Easy | DFS | |
348348
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Java](./Java/Balanced-Binary-Tree.java) | _O(n)_ | _O(n)_ | Easy | DFS | |
349349
| 1376 | [ Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [C++](./C++/Cherry-Pickup-II.cpp) | _O(n)_ | _O(n)_ | Medium | DFS | |
350+
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [C++](./C++/number-of-islands.cpp) | _O(m * n)_ | _O(m * n)_ | Medium | DFS | |
350351

351-
|<br/>
352-
352+
<br/>
353353
<div align="right">
354354
<b><a href="#algorithms">⬆️ Back to Top</a></b>
355355
</div>
@@ -500,6 +500,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
500500
| [Sachin_Upadhyay](https://github.com/sachsbu) <br> <img src="https://avatars.githubusercontent.com/u/24941685?v=4" width="100" height="100"> | India | Java | [GitHub](https://github.com/sachsbu) |
501501
| [Amisha Sahu](https://github.com/Amisha328) <br> <img src = "https://avatars.githubusercontent.com/u/58816552?v=4" width="100" height="100"> | India | C++ | [CodeChef](https://www.codechef.com/users/amisha328)<br/>[LeetCode](https://leetcode.com/Mishi328/)<br/>[HackerRank](https://www.hackerrank.com/amishasahu328)
502502
<br/>
503+
| [Shrimadh V Rao](https://github.com/Shrimadh) <br> <img src="https://avatars.githubusercontent.com/u/64469917?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Shrimadh)
503504
<div align="right">
504505
<b><a href="#algorithms">⬆️ Back to Top</a></b>
505506
</div>

0 commit comments

Comments
 (0)