From 3ef1bb6b4dc0c5f0f94d26cfe3b1e7c866ceefd1 Mon Sep 17 00:00:00 2001 From: Shrimadh Rao Date: Wed, 18 Aug 2021 16:24:37 +0530 Subject: [PATCH 1/5] added number of islands question --- C++/number-of-islands.cpp | 41 +++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 42 insertions(+) create mode 100644 C++/number-of-islands.cpp diff --git a/C++/number-of-islands.cpp b/C++/number-of-islands.cpp new file mode 100644 index 00000000..16c64ade --- /dev/null +++ b/C++/number-of-islands.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + bool visited[300][300]; + int r[4] = {-1,0,0,1}; + int c[4] = {0,-1,1,0}; + bool val(int row,int col,vector>& grid,int M,int N) + { + return (row=0 && col>=0 && !visited[row][col] && grid[row][col]=='1'); + } + void dfs(int i,int j,vector>& 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>& grid) { + int m = grid.size(); + int n = grid[0].size(); + memset(visited,0,sizeof(visited)); + int island_count = 0; + for(int i=0;i | India | Java | [GitHub](https://github.com/sachsbu) | | [Amisha Sahu](https://github.com/Amisha328)
| India | C++ | [CodeChef](https://www.codechef.com/users/amisha328)
[LeetCode](https://leetcode.com/Mishi328/)
[HackerRank](https://www.hackerrank.com/amishasahu328)
+| [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) From d025a552157822c0f071ea84daf0c3af69612852 Mon Sep 17 00:00:00 2001 From: Shrimadh <64469917+Shrimadh@users.noreply.github.com> Date: Wed, 18 Aug 2021 16:32:44 +0530 Subject: [PATCH 2/5] Update number-of-islands.cpp --- C++/number-of-islands.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/C++/number-of-islands.cpp b/C++/number-of-islands.cpp index 16c64ade..bffdc2c2 100644 --- a/C++/number-of-islands.cpp +++ b/C++/number-of-islands.cpp @@ -3,10 +3,14 @@ class Solution { 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>& grid,int M,int N) { return (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>& grid, int M, int N) { visited[i][j] = true; @@ -20,6 +24,7 @@ class Solution { } } } + int numIslands(vector>& grid) { int m = grid.size(); int n = grid[0].size(); @@ -32,10 +37,13 @@ class Solution { if(!visited[i][j] && grid[i][j]=='1') { dfs(i,j,grid,m,n); - island_count++; + 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; } -}; \ No newline at end of file +}; +/* 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.*/ From 6264d3b07d2fa4ce8e2d2dd084ba09dc1eb86c5c Mon Sep 17 00:00:00 2001 From: Shrimadh <64469917+Shrimadh@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:01:09 +0530 Subject: [PATCH 3/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f8d9ec0..a6151a3b 100644 --- a/README.md +++ b/README.md @@ -347,7 +347,7 @@ 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 | | |
From cae3caa6937f541c8e96ce5b479781dec6ebae45 Mon Sep 17 00:00:00 2001 From: Shrimadh <64469917+Shrimadh@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:04:22 +0530 Subject: [PATCH 4/5] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a6151a3b..a551921d 100644 --- a/README.md +++ b/README.md @@ -348,8 +348,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 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 | | -|
+| +
From c013b24ec95569af247f683d1ccb6cf7f2a4bd9e Mon Sep 17 00:00:00 2001 From: Shrimadh <64469917+Shrimadh@users.noreply.github.com> Date: Sun, 22 Aug 2021 20:06:56 +0530 Subject: [PATCH 5/5] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a551921d..e0486c1a 100644 --- a/README.md +++ b/README.md @@ -348,7 +348,6 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 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 | | -|