Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number of Islands question in C++ language. Pull Request for Issue #77 #188

Merged
merged 5 commits into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions C++/number-of-islands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution {
public:
bool visited[300][300];
int r[4] = {-1,0,0,1};
int c[4] = {0,-1,1,0};

//Checks if the given row and col value are valid and if the cell is visited and if the cell contains '1' or not.
bool val(int row,int col,vector<vector<char>>& grid,int M,int N)
{
return (row<M && col<N && row>=0 && col>=0 && !visited[row][col] && grid[row][col]=='1');
}

//Dfs function for exploring the surrounding cells
void dfs(int i,int j,vector<vector<char>>& grid, int M, int N)
{
visited[i][j] = true;
for(int a=0;a<4;a++)
{
int row = i + r[a];
int col = j + c[a];
if(val(row,col,grid,M,N))
{
dfs(row,col,grid,M,N);
}
}
}

int numIslands(vector<vector<char>>& grid) {
int m = grid.size();
int n = grid[0].size();
memset(visited,0,sizeof(visited));
int island_count = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(!visited[i][j] && grid[i][j]=='1')
{
dfs(i,j,grid,m,n);
island_count++; // Island count is incremented when there is a cell that has not been visited and contains a '1'.
//Dfs function marks the other connected '1's in the visited matrix.
}
}
}
return island_count;
}
};
/* Time complexity : O(m x n) as each cell is accessed one time.
Space complexity : O(m x n) as the visited matrix contains m x n elements.*/
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [Java](./Java/path-sum.java) | _O(n)_ | _O(n)_ | Easy | DFS | |
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Java](./Java/Balanced-Binary-Tree.java) | _O(n)_ | _O(n)_ | Easy | DFS | |
| 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 | |
| 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 | |

|<br/>

<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
Expand Down Expand Up @@ -500,6 +500,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
| [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) |
| [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)
<br/>
| [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)
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
Expand Down