-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0695.cpp
More file actions
30 lines (26 loc) · 1.08 KB
/
0695.cpp
File metadata and controls
30 lines (26 loc) · 1.08 KB
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
class Solution {
private:
int evaluateIslandArea(vector<vector<int>> const &grid, vector<vector<bool>> &marks, int i, int j) {
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size()) return 0;
if (grid[i][j] == 0 || marks[i][j]) return 0;
marks[i][j] = true;
int area = 0;
vector<pair<int,int>> directions = {{-1,0}, {0,1}, {1,0}, {0,-1}};
for (auto d : directions)
area += evaluateIslandArea(grid, marks, i + d.first, j + d.second);
return area + 1;
}
public:
int maxAreaOfIsland(vector<vector<int>> &grid) {
vector<vector<bool>> marks(grid.size(), vector<bool>(grid[0].size(), false));
int maxArea = 0;
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[0].size(); j++) {
if (!marks[i][j] && grid[i][j]) {
int currentArea = evaluateIslandArea(grid, marks, i, j);
maxArea = max(currentArea, maxArea);
}
}
}
return maxArea;
}