notes/OJ notes/pages/Leetcode Max-Area-of-Island.md

265 lines
5.6 KiB
Markdown
Raw Permalink Normal View History

2022-07-15 10:12:32 +08:00
# Leetcode Max-Area-of-Island
#### 2022-07-15 10:00
> ##### Algorithms:
2022-09-03 15:41:36 +08:00
>
> #algorithm
>
2022-07-15 10:12:32 +08:00
> ##### Data structures:
2022-09-03 15:41:36 +08:00
>
> #DS #vector_2d
>
2022-07-15 10:12:32 +08:00
> ##### Difficulty:
2022-09-03 15:41:36 +08:00
>
2022-09-06 20:22:48 +08:00
> #coding_problem #difficulty_medium
2022-09-03 15:41:36 +08:00
>
2022-07-15 10:12:32 +08:00
> ##### Additional tags:
2022-09-03 15:41:36 +08:00
>
> #leetcode
>
2022-07-15 10:12:32 +08:00
> ##### Revisions:
2022-09-03 15:41:36 +08:00
>
2022-07-15 10:12:32 +08:00
> N/A
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-07-15 10:12:32 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-07-15 10:12:32 +08:00
- [Link to problem](https://leetcode.com/problems/max-area-of-island/)
2022-09-03 15:41:36 +08:00
---
2022-07-15 10:12:32 +08:00
### Problem
You are given an `m x n` binary matrix `grid`. An island is a group of `1`'s (representing land) connected **4-directionally** (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The **area** of an island is the number of cells with a value `1` in the island.
Return _the maximum **area** of an island in_ `grid`. If there is no island, return `0`.
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2021/05/01/maxarea1-grid.jpg)
```
**Input:** grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
**Output:** 6
**Explanation:** The answer is not 11, because the island must be connected 4-directionally.
```
**Example 2:**
```
**Input:** grid = [[0,0,0,0,0,0,0,0]]
**Output:** 0
```
#### Constraints
2022-09-03 15:41:36 +08:00
- `m == grid.length`
- `n == grid[i].length`
- `1 <= m, n <= 50`
- `grid[i][j]` is either `0` or `1`.
2022-07-15 10:12:32 +08:00
### Thoughts
> [!summary]
2022-09-03 15:41:36 +08:00
> This is a search problem, can be implemented using #DFS or #BFS
2022-07-15 10:12:32 +08:00
Simple O(mn) solution.
We keep track of pile we visited, using:
2022-09-03 15:41:36 +08:00
2022-07-15 10:12:32 +08:00
- an 2-d bool vector visited(which is the one I'm using)
- change the value of pile to 0
2022-07-16 08:59:20 +08:00
and check for:
2022-09-03 15:41:36 +08:00
2022-07-16 08:59:20 +08:00
- x, y is not OOB
- x, y is in a island -> (value == 1)
- x, y is not visited (see above)
2022-07-15 10:12:32 +08:00
And for each unvisited pile, run DFS or BFS to check the size, and keep track of max size with a variable
### Solution
BFS
2022-09-03 15:41:36 +08:00
2022-07-15 10:12:32 +08:00
```cpp
class Solution {
int getSize(vector<vector<int>> &grid, int m, int n, int x, int y,
vector<vector<bool>> &visited) {
queue<pair<int, int>> todo;
todo.push({x, y});
int r, c;
int size = 0;
while (!todo.empty()) {
r = todo.front().first;
c = todo.front().second;
todo.pop();
if (visited[r][c] || grid[r][c] != 1) {
// Visited this place, or the color is not 1
continue;
}
size++;
visited[r][c] = true;
if (r > 0) {
todo.push({r - 1, c});
}
if (r < m - 1) {
todo.push({r + 1, c});
}
if (c > 0) {
todo.push({r, c - 1});
}
if (c < n - 1) {
todo.push({r, c + 1});
}
}
return size;
}
public:
int maxAreaOfIsland(vector<vector<int>> &grid) {
int m = grid.size(), n = grid[0].size();
int maxSize = 0;
vector<vector<bool>> visited;
for (int i = 0; i < m; i++) {
visited.push_back({});
for (int j = 0; j < n; j++) {
visited.back().push_back(false);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 && !visited[i][j]) {
maxSize = max(maxSize, getSize(grid, m, n, i, j, visited));
}
}
}
return maxSize;
}
};
```
DFS iterative (simply change from queue to stack in BFS)
2022-09-03 15:41:36 +08:00
2022-07-15 10:12:32 +08:00
```cpp
class Solution {
// DFS iterative
int getSize(vector<vector<int>> &grid, int m, int n, int x, int y,
vector<vector<bool>> &visited) {
stack<pair<int, int>> todo;
todo.push({x, y});
int r, c;
int size = 0;
while (!todo.empty()) {
r = todo.top().first;
c = todo.top().second;
todo.pop();
if (visited[r][c] || grid[r][c] != 1) {
// Visited this place, or the color is not 1
continue;
}
size++;
visited[r][c] = true;
if (r > 0) {
todo.push({r - 1, c});
}
if (r < m - 1) {
todo.push({r + 1, c});
}
if (c > 0) {
todo.push({r, c - 1});
}
if (c < n - 1) {
todo.push({r, c + 1});
}
}
return size;
}
public:
int maxAreaOfIsland(vector<vector<int>> &grid) {
int m = grid.size(), n = grid[0].size();
int maxSize = 0;
vector<vector<bool>> visited;
for (int i = 0; i < m; i++) {
visited.push_back({});
for (int j = 0; j < n; j++) {
visited.back().push_back(false);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 && !visited[i][j]) {
maxSize = max(maxSize, getSize(grid, m, n, i, j, visited));
}
}
}
return maxSize;
}
};
```
2022-07-16 08:59:20 +08:00
DFS recursive
2022-09-03 15:41:36 +08:00
2022-07-16 08:59:20 +08:00
```cpp
class Solution {
// DFS recursive
int getSize(vector<vector<int>> &grid, int m, int n, int x, int y,
vector<vector<bool>> &visited) {
if (x >= 0 && x < m && y >= 0 && y < n && !visited[x][y] &&
grid[x][y] == 1) {
visited[x][y] = true;
return 1 + getSize(grid, m, n, x + 1, y, visited) +
getSize(grid, m, n, x - 1, y, visited) +
getSize(grid, m, n, x, y + 1, visited) +
getSize(grid, m, n, x, y - 1, visited);
} else {
return 0;
}
}
public:
int maxAreaOfIsland(vector<vector<int>> &grid) {
int m = grid.size(), n = grid[0].size();
int maxSize = 0;
vector<vector<bool>> visited;
for (int i = 0; i < m; i++) {
visited.push_back({});
for (int j = 0; j < n; j++) {
visited.back().push_back(false);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 && !visited[i][j]) {
maxSize = max(maxSize, getSize(grid, m, n, i, j, visited));
}
}
}
return maxSize;
}
};
2022-09-03 15:41:36 +08:00
```