-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax Area of Island.cpp
38 lines (31 loc) · 984 Bytes
/
Max Area of Island.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
class Solution {
int dfs(vector<vector<int>>& grid,int i,int j)
{
int row = grid.size();
int column = grid[0].size();
if(i<0 || j<0 || i>=row || j>=column || grid [i][j] != 1)
{
return 0;
}
grid[i][j] = 2;
return(1+dfs(grid,i+1,j) + dfs(grid,i-1,j) + dfs(grid,i,j+1) + dfs(grid,i,j-1));
// 1st DFS = Bottom || 2nd DFS = Top || 3rd DFS = Right || 4th DFS = Left...!!
}
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int row = grid.size();
int column = grid[0].size();
int result = 0;
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
if(grid[i][j] == 1)
{
result = max(result,dfs(grid,i,j));
}
}
}
return result;
}
};