Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 966beaa

Browse files
authored
Added task 695.
1 parent 8795c15 commit 966beaa

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0601_0700.s0695_max_area_of_island;
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
4+
5+
public class Solution {
6+
public int maxAreaOfIsland(int[][] grid) {
7+
if (grid == null || grid.length == 0) {
8+
return 0;
9+
}
10+
int m = grid.length;
11+
int n = grid[0].length;
12+
int max = 0;
13+
for (int i = 0; i < m; i++) {
14+
for (int j = 0; j < n; j++) {
15+
if (grid[i][j] == 1) {
16+
int area = dfs(grid, i, j, m, n, 0);
17+
max = Math.max(area, max);
18+
}
19+
}
20+
}
21+
return max;
22+
}
23+
24+
private int dfs(int[][] grid, int i, int j, int m, int n, int area) {
25+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
26+
return area;
27+
}
28+
grid[i][j] = 0;
29+
area++;
30+
area = dfs(grid, i + 1, j, m, n, area);
31+
area = dfs(grid, i, j + 1, m, n, area);
32+
area = dfs(grid, i - 1, j, m, n, area);
33+
area = dfs(grid, i, j - 1, m, n, area);
34+
return area;
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
695\. Max Area of Island
2+
3+
Medium
4+
5+
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.
6+
7+
The **area** of an island is the number of cells with a value `1` in the island.
8+
9+
Return _the maximum **area** of an island in_ `grid`. If there is no island, return `0`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/05/01/maxarea1-grid.jpg)
14+
15+
**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]]
16+
17+
**Output:** 6
18+
19+
**Explanation:** The answer is not 11, because the island must be connected 4-directionally.
20+
21+
**Example 2:**
22+
23+
**Input:** grid = [[0,0,0,0,0,0,0,0]]
24+
25+
**Output:** 0
26+
27+
**Constraints:**
28+
29+
* `m == grid.length`
30+
* `n == grid[i].length`
31+
* `1 <= m, n <= 50`
32+
* `grid[i][j]` is either `0` or `1`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0601_0700.s0695_max_area_of_island;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maxAreaOfIsland() {
11+
int[][] grid = {
12+
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
13+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
14+
{0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
15+
{0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},
16+
{0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
17+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
18+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
19+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}
20+
};
21+
assertThat(new Solution().maxAreaOfIsland(grid), equalTo(6));
22+
}
23+
24+
@Test
25+
void maxAreaOfIsland2() {
26+
int[][] grid = {{0, 0, 0, 0, 0, 0, 0, 0}};
27+
assertThat(new Solution().maxAreaOfIsland(grid), equalTo(0));
28+
}
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /