-
Notifications
You must be signed in to change notification settings - Fork 5
/
695. Max Area of Island.java
63 lines (63 loc) · 2.03 KB
/
695. Max Area of Island.java
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
class Solution {
private class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
private int[] dx = new int[] { 1, -1, 0, 0 };
private int[] dy = new int[] { 0, 0, 1, -1 };
private int maxarea = 0;
public void area(int[][] grid, Pair start, boolean[][] visited){
int n = grid.length;
int m = grid[0].length;
Queue<Pair> queue = new LinkedList<>();
if(visited[start.x][start.y] == true) return;
queue.add(start);
int curarea = 1;
while (queue.isEmpty() == false) {
Pair p = queue.remove();
visited[p.x][p.y] = true;
for (int k = 0; k < 4; k++) {
int nx = p.x + dx[k];
int ny = p.y + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
if (grid[nx][ny] == 1 && visited[nx][ny] == false) {
curarea++;
visited[nx][ny] = true;
queue.add(new Pair(nx, ny));
}
}
}
}
// System.out.println( start.x + " " + start.y + " " + curarea);
this.maxarea = Math.max(curarea, this.maxarea);
}
public int maxAreaOfIsland(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
boolean[][] visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1) {
Pair start = new Pair(i, j);
area(grid, start, visited);
}
}
}
return this.maxarea;
}
}