-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax-area-of-island.cpp
More file actions
76 lines (64 loc) · 1.76 KB
/
max-area-of-island.cpp
File metadata and controls
76 lines (64 loc) · 1.76 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 695. Max Area of Island (7/17/57643)
// Runtime: 23 ms (1.68%) Memory: 30.62 MB (22.98%)
#include <queue>
class Solution {
struct Cell
{
int x;
int y;
bool operator<(const Cell& other) const
{
if (x != other.x) return x < other.x;
return y < other.y;
}
};
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
ROWS = grid.size();
COLS = grid[0].size();
int maxArea = 0;
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
if(grid[i][j] == 1
&& processed.find({i,j}) == processed.end())
{
const auto area = bfs(grid, {i, j});
maxArea = std::max(maxArea, area);
}
}
}
return maxArea;
}
int bfs(const auto& grid, const Cell& start)
{
frontier.push(start);
processed.insert(start);
int area = 0;
while(!frontier.empty())
{
const auto curr = frontier.front(); frontier.pop();
area++;
for(const auto& [dx, dy] : directions)
{
const Cell next{curr.x + dx, curr.y + dy};
if(next.x >= 0 && next.x < ROWS &&
next.y >= 0 && next.y < COLS &&
processed.find(next) == processed.end()&&
grid[next.x][next.y] == 1)
{
frontier.push(next);
processed.insert(next);
}
}
}
return area;
}
private:
int ROWS;
int COLS;
std::vector<Cell> directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
std::queue<Cell> frontier;
std::set<Cell> processed;
};