-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path695.cpp
More file actions
31 lines (31 loc) · 937 Bytes
/
695.cpp
File metadata and controls
31 lines (31 loc) · 937 Bytes
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
class Solution {
public:
int M;
int N;
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int dfs(int x, int y, vector<vector<int>>& grid) {
grid[x][y] = 0;
int res = 1;
for (auto& direction : directions) {
int xNext = x + direction.first;
int yNext = y + direction.second;
if (xNext < 0 || xNext >= M || yNext < 0 || yNext >= N) continue;
if (grid[xNext][yNext] == 0) continue;
res += dfs(xNext, yNext, grid);
}
return res;
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int res = 0;
M = grid.size();
N = grid[0].size();
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (grid[i][j] == 1) {
res = max(res, dfs(i, j, grid));
}
}
}
return res;
}
};