forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-sub-islands.cpp
36 lines (34 loc) · 1.05 KB
/
count-sub-islands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Time: O(m * n)
// Space: O(1)
class Solution {
public:
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
int result = 0;
for (int i = 0; i < size(grid2); ++i) {
for (int j = 0; j < size(grid2[0]); ++j) {
if (grid2[i][j]) {
result += dfs(grid1, &grid2, i, j);
}
}
}
return result;
}
private:
int dfs(const vector<vector<int>>& grid1,
vector<vector<int>> *grid2,
int i, int j) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0},
{0, -1}, {-1, 0}};
if (!(0 <= i && i < size(*grid2) &&
0 <= j && j < size((*grid2)[0]) &&
(*grid2)[i][j] == 1)) {
return 1;
}
(*grid2)[i][j] = 0;
int result = grid1[i][j];
for (const auto& [di, dj] : directions) {
result &= dfs(grid1, grid2, i + di, j + dj);
}
return result;
}
};