-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
53 lines (40 loc) · 1.38 KB
/
Copy pathSolution.java
File metadata and controls
53 lines (40 loc) · 1.38 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
import java.util.LinkedList;
import java.util.Queue;
class Solution {
private static final int[][] directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public int maxAreaOfIsland(int[][] grid) {
int n = grid.length, m = grid[0].length;
boolean[][] visited = new boolean[n][m];
int max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (visited[i][j] || grid[i][j] == 0) {
continue;
}
max = Math.max(max, findArea(i, j, grid, visited));
}
}
return max;
}
private int findArea(int i, int j, int[][] grid, boolean[][] visited) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{i, j});
visited[i][j] = true;
int area = 1;
while (!queue.isEmpty()) {
int[] curr = queue.poll();
for (int[] dir : directions) {
int a = curr[0] + dir[0], b = curr[1] + dir[1];
if (a < 0 || a >= grid.length || b < 0 || b >= grid[0].length) {
continue;
}
if (grid[a][b] == 1 && !visited[a][b]) {
queue.add(new int[]{a, b});
area++;
visited[a][b] = true;
}
}
}
return area;
}
}