Skip to content

Commit e8c3199

Browse files
committed
Prob: 167 Count number of islands
1 parent 4aa957f commit e8c3199

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 166 |
9+
| Total Problems | 167 |
1010

1111
</center>
1212

@@ -236,5 +236,5 @@ Include contains single header implementation of data structures and some algori
236236
| Given a string, sort it in decreasing order based on the frequency of characters.For example: <ul><li>Input: cccbbbbaa Output: bbbcccaa</li></ul>| [sortCharByFrequency.cpp](leet_code_problems/sortCharByFrequency.cpp)|
237237
|Product of Array Except Self. Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].| [product_except_self.cpp](leet_code_problems/product_except_self.cpp)|
238238
|Given a sorted array, remove duplicates in place and return the new length. It doesn't matter what is in array beyond the unique elements size. Expected O(1) space and O(n) time complexity.| [remove_duplicates.cpp](leet_code_problems/remove_duplicates.cpp) |
239-
239+
| Count the number of islands in a grid. Given a grid representing 1 as land body, and 0 as water body, determine the number of islands (more details in problem comments)|[count_islands.cpp](leet_code_problems/count_islands.cpp)|
240240

leet_code_problems/count_islands.cpp

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* In a given 2D grid, 1 represents land and 0 represents water body.
3+
* We need to count number of islands.
4+
* An island is surrounded by connecting neighbouring lands vertically
5+
* or horizontally.
6+
* Assume: That everything around the grid is water.
7+
*
8+
* Approach: We can solve this problem with Depth/Breadth first search.
9+
*
10+
* With Depth first search:
11+
* Consider a land body, and start a depth first search, and mark all the
12+
* connected land bodies to it as water bodies (i.e. mark all connected 1's
13+
* as 0 to mark them visited)
14+
* In this case, number of islands for which DFS was started is the total
15+
* count of islands.
16+
* Similar idea could be used for BFS.
17+
*
18+
* Example:
19+
*
20+
* 1 1 0 0
21+
* 1 0 1 0
22+
* 1 0 0 1
23+
*
24+
* There are 3 islands in above grid.
25+
*/
26+
27+
#include <iostream>
28+
#include <vector>
29+
30+
bool is_in_grid(const std::vector<std::vector<int> >& grid, int i, int j)
31+
{
32+
int m = grid.size();
33+
int n = grid[0].size();
34+
return (i >= 0 && i < m && j >= 0 && j < n);
35+
}
36+
37+
void depth_first_search(std::vector<std::vector<int> >& grid, int i, int j)
38+
{
39+
int m = grid.size();
40+
int n = grid[0].size();
41+
42+
if (i < 0 || i >= m || j < 0 || j >= n) {
43+
return;
44+
}
45+
grid[i][j] = 0;
46+
if (i + 1 < m && grid[i + 1][j] == 1) {
47+
depth_first_search(grid, i + 1, j);
48+
}
49+
if (i - 1 >= 0 && grid[i - 1][j] == 1) {
50+
depth_first_search(grid, i - 1, j);
51+
}
52+
if (j + 1 < n && grid[i][j + 1] == 1) {
53+
depth_first_search(grid, i, j + 1);
54+
}
55+
if (j - 1 >= 0 && grid[i][j - 1] == 1) {
56+
depth_first_search(grid, i, j - 1);
57+
}
58+
}
59+
60+
int number_of_islands(std::vector<std::vector<int>>& grid)
61+
{
62+
int m = grid.size();
63+
if (m == 0) {
64+
return 0;
65+
}
66+
int n = grid[0].size();
67+
int total_islands = 0;
68+
for ( int i = 0; i < m; ++i) {
69+
for ( int j = 0; j < n; ++j) {
70+
if (grid[i][j] == 1) {
71+
++total_islands;
72+
depth_first_search(grid, i, j);
73+
}
74+
}
75+
}
76+
return total_islands;
77+
}
78+
79+
80+
int main()
81+
{
82+
std::vector<std::vector<int>> grid = {
83+
{1, 1, 0, 0, 0},
84+
{1, 1, 0, 0, 0},
85+
{0, 0, 1, 0, 0},
86+
{0, 0, 0, 1, 1}
87+
};
88+
89+
for (int i = 0; i < grid.size(); ++i) {
90+
for (int j = 0; j < grid[0].size(); ++j) {
91+
std::cout << grid[i][j] << " ";
92+
}
93+
std::cout << std::endl;
94+
}
95+
std::cout << "Total number of islands in the grid is :"
96+
<< number_of_islands(grid) << std::endl;
97+
return 0;
98+
}

sort_search_problems/closestPairSorted.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include <cmath>
2121

2222
std::pair<int,int> closestPair( std::vector<int> & vec1, std::vector<int> & vec2, int x ) {
23-
size_t left = 0;
24-
size_t right = vec2.size() - 1;
23+
int left = 0;
24+
int right = vec2.size() - 1;
2525
int minDiff = std::numeric_limits<int>::max();
2626
int leftRes, rightRes;
2727
while (left < vec1.size() && right >= 0 ) {

0 commit comments

Comments
 (0)