Skip to content

Commit 001f60d

Browse files
committed
Add Problem 1
1 parent c4a2cfb commit 001f60d

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

Island Count/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Count the number of islands
2+
3+
Given a 2D array `binaryMatrix` of 0s and 1s, implement a function `getNumberOfIslands` that returns the number of islands of 1s in `binaryMatrix`.
4+
5+
An island is defined as a group of adjacent values that are all 1s. A cell in `binaryMatrix` is considered adjacent to another cell if they are next to each either on the same row or column. Note that two values of 1 are **not** part of the same island if they’re sharing only a mutual “corner” (i.e. they are diagonally neighbors).
6+
7+
Explain and code the most efficient solution possible and analyze its time and space complexities.
8+
9+
### Example:
10+
```
11+
input: binaryMatrix = [ [0, 1, 0, 1, 0],
12+
[0, 0, 1, 1, 1],
13+
[1, 0, 0, 1, 0],
14+
[0, 1, 1, 0, 0],
15+
[1, 0, 1, 0, 1] ]
16+
17+
output: 6 # since this is the number of islands in binaryMatrix.
18+
# See all 6 islands color-coded below.
19+
```
20+
21+
![exampleImage](example.png)

Island Count/example.png

22.3 KB
Loading

Island Count/solution.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
#include <utility>
5+
6+
using namespace std;
7+
8+
int dRow[4] = {-1, 0, 0, +1};
9+
int dCol[4] = {0, +1, -1, 0};
10+
11+
/*
12+
C2
13+
C1 C3 C4
14+
C5
15+
*/
16+
17+
bool isSafe(int row, int col, int R, int C) {
18+
if (row < 0 || row >= R || col < 0 || col >= C) {
19+
return false;
20+
}
21+
return true;
22+
}
23+
24+
void bfs(int cr, int cc, const vector<vector<int>>& binaryMatrix,
25+
vector<vector<bool>> &visited) {
26+
queue<pair<int, int>> q;
27+
q.push({cr, cc});
28+
int R = binaryMatrix.size();
29+
int C = binaryMatrix[0].size();
30+
while (!q.empty()) {
31+
// pop from the queue to get front cell
32+
int curRow = q.front().first;
33+
int curCol = q.front().second;
34+
visited[curRow][curCol] = true;
35+
q.pop();
36+
37+
for (int k = 0; k < 4; ++k) {
38+
int nextRow = curRow + dRow[k];
39+
int nextCol = curCol + dCol[k];
40+
if (isSafe(nextRow, nextCol, R, C)) {
41+
bool shouldVisit = !visited[nextRow][nextCol] &&
42+
(binaryMatrix[nextRow][nextCol] == 1);
43+
if (shouldVisit) {
44+
q.push({nextRow, nextCol});
45+
}
46+
}
47+
}
48+
}
49+
}
50+
51+
int getNumberOfIslands( const vector<vector<int>>& binaryMatrix )
52+
{
53+
int countOfIslands = 0;
54+
55+
int rows = binaryMatrix.size();
56+
int cols = binaryMatrix[0].size();
57+
vector<vector<bool>> visited(rows, vector<bool>(cols, false));
58+
59+
for (int curRow = 0; curRow < rows; ++curRow) {
60+
for (int curCol = 0; curCol < cols; ++curCol) {
61+
// check if not visited and equal to 1
62+
bool shouldVisit = !visited[curRow][curCol] && (binaryMatrix[curRow][curCol] == 1);
63+
if (shouldVisit) {
64+
bfs(curRow, curCol, binaryMatrix, visited);
65+
++countOfIslands;
66+
}
67+
}
68+
}
69+
return countOfIslands;
70+
}
71+
72+
int main() {
73+
74+
vector<vector<int>> matrix = {{1, 1, 1, 1, 0}};
75+
76+
cout << getNumberOfIslands(matrix) << endl;
77+
return 0;
78+
79+
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
# Coding-Interview-Problems
22
This repository contains the coding interview problems along with solutions.
3+
4+
| Problem | Solution | Code | Category | Difficulty |
5+
|---------------|----------|------|----------|------------|
6+
| Island Count | | | | |
7+
| | | | | |
8+
| | | | | |

0 commit comments

Comments
 (0)