-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakingALargeIsland.cpp
55 lines (51 loc) · 1.72 KB
/
makingALargeIsland.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
// Source: https://leetcode.com/problems/making-a-large-island/
// Author: Miao Zhang
// Date: 2021-03-15
class Solution {
public:
int largestIsland(vector<vector<int>>& grid) {
color = 1;
m = grid.size();
n = grid[0].size();
unordered_map<int, int> areas; // color: area
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
++color;
areas[color] = getArea(grid, i, j);
res = max(res, areas[color]);
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
int area = 1;
for (int color: set<int>{getColor(grid, i, j - 1),
getColor(grid, i, j + 1),
getColor(grid, i - 1, j),
getColor(grid, i + 1, j)}) {
area += areas[color];
}
res = max(res, area);
}
}
}
return res;
}
private:
int m;
int n;
int color;
int getArea(vector<vector<int>>& grid, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] != 1) return 0;
grid[i][j] = color;
return 1 + getArea(grid, i - 1, j) + getArea(grid, i + 1, j) \
+ getArea(grid, i, j - 1) + getArea(grid, i, j + 1);
}
int getColor(vector<vector<int>>& grid, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n) return 0;
return grid[i][j];
}
};