-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC0695.cpp
More file actions
executable file
·43 lines (36 loc) · 1005 Bytes
/
LC0695.cpp
File metadata and controls
executable file
·43 lines (36 loc) · 1005 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
39
40
41
42
43
/*
Problem Statement: https://leetcode.com/problems/max-area-of-island/
Time: O(m • n)
Space: O(m • n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int area, max_area = 0, m = grid.size(), n = grid[0].size();
vector<vector<bool>> visited(m, vector<bool>(n));
// traverse directions easily
vector<int> xdir = {-1, 0, 1, 0};
vector<int> ydir = {0, -1, 0, 1};
// helper function
function<void(int, int)> dfs = [&](int x, int y) {
// base case
if (x < 0 || x == m || y < 0 || y == n || grid[x][y] == 0 || visited[x][y])
return;
area++;
visited[x][y] = true;
for (int k = 0; k < xdir.size(); k++) {
int new_x = x + xdir[k], new_y = y + ydir[k];
dfs(new_x, new_y);
}
};
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (!visited[i][j]) {
area = 0;
dfs(i, j);
max_area = max(area, max_area);
}
return max_area;
}
};