-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path695.cpp
More file actions
30 lines (30 loc) · 801 Bytes
/
695.cpp
File metadata and controls
30 lines (30 loc) · 801 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
class Solution {
public:
int curr;
void dfs(vector<vector<int>>& grid, int i, int j){
if(i>=grid.size() || j>=grid[0].size() || i<0 || j<0 || grid[i][j]==0)
return;
curr++;
grid[i][j] = 0;
dfs(grid,i+1,j);
dfs(grid,i,j+1);
dfs(grid,i-1,j);
dfs(grid,i,j-1);
return;
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
if(grid.empty())
return 0;
int res = INT_MIN;
for(int i=0; i<grid.size(); i++){
for(int j=0 ;j<grid[0].size() ;j++){
if(grid[i][j]==1){
curr = 0;
dfs(grid,i,j);
res = max(res,curr);
}
}
}
return (res==INT_MIN?0:res);
}
};