-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
38 lines (33 loc) · 741 Bytes
/
solution.cpp
File metadata and controls
38 lines (33 loc) · 741 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
32
33
34
35
36
37
38
//
// Created by mingyi on 09.05.20.
//
#include <vector>
using namespace std;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int area(int i, int j, int I, int J, vector<vector<int>> &grid) {
if (i < 0 || j < 0 || i >= I || j >= J || !grid[i][j]) {
return 0;
}
grid[i][j] = 0;
int ret = 1;
for (int k = 0; k < 4; k++) {
ret += area(i + dx[k], j + dy[k], I, J, grid);
}
return ret;
}
int maxAreaOfIsland(vector<vector<int>> &grid) {
if (grid.empty()) {
return 0;
}
int ret{0};
const int I = grid.size(), J = grid[0].size();
for (int i = 0; i < I; i++) {
for (int j = 0; j < J; j++) {
if (grid[i][j]) {
ret = max(area(i, j, I, J, grid), ret);
}
}
}
return ret;
}