-
Notifications
You must be signed in to change notification settings - Fork 10
/
making-a-large-island.cpp
60 lines (56 loc) · 1.82 KB
/
making-a-large-island.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Time: O(n^2)
// Space: O(n^2)
class Solution {
public:
int largestIsland(vector<vector<int>>& grid) {
int result = 0;
unordered_map<int, int> area;
int index = 2;
for (int r = 0; r < grid.size(); ++r) {
for (int c = 0; c < grid[r].size(); ++c) {
if (grid[r][c] != 1) {
continue;
}
area[index] = dfs(r, c, index, &grid);
result = max(result, area[index++]);
}
}
for (int r = 0; r < grid.size(); ++r) {
for (int c = 0; c < grid[r].size(); ++c) {
if (grid[r][c] != 0) {
continue;
}
unordered_set<int> seen;
for (const auto& d :directions) {
int nr = r + d.first, nc = c + d.second;
if (0 <= nr && nr < grid.size() &&
0 <= nc && nc < grid[0].size() &&
grid[nr][nc] > 1) {
seen.emplace(grid[nr][nc]);
}
}
int sum = 0;
for (const auto& i : seen) {
sum += area[i];
}
result = max(result, 1 + sum);
}
}
return result;
}
private:
const vector<pair<int, int>> directions{{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int dfs(int r, int c, int index, vector<vector<int>> *grid) {
if (!(0 <= r && r < grid->size() &&
0 <= c && c < (*grid)[0].size() &&
(*grid)[r][c] == 1)) {
return 0;
}
int result = 1;
(*grid)[r][c] = index;
for (const auto& d :directions) {
result += dfs(r + d.first, c + d.second, index, grid);
}
return result;
}
};