-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy path695_Max_Area_of_Island.java
57 lines (55 loc) · 1.95 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
/*class Solution {
int[][] grid;
public int area(int r, int c) {
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || grid[r][c] == 0)
return 0;
grid[r][c] = 0;
return (1 + area(r + 1, c) + area(r - 1, c)
+ area(r, c - 1) + area(r, c + 1));
}
public int maxAreaOfIsland(int[][] grid) {
this.grid = grid;
int ans = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[0].length; c++) {
ans = Math.max(ans, area(r, c));
}
}
return ans;
}
}*/
class Solution {
// DFS and you can implement BFS with queue
public int maxAreaOfIsland(int[][] grid) {
int[] dr = new int[]{1, -1, 0, 0};
int[] dc = new int[]{0, 0, 1, -1};
int ans = 0;
for (int r0 = 0; r0 < grid.length; r0++) {
for (int c0 = 0; c0 < grid[0].length; c0++) {
if (grid[r0][c0] == 1) {
int shape = 0;
Stack<int[]> stack = new Stack();
stack.push(new int[]{r0, c0});
grid[r0][c0] = 0;
while (!stack.empty()) {
int[] node = stack.pop();
int r = node[0], c = node[1];
shape++;
for (int k = 0; k < 4; k++) {
int nr = r + dr[k];
int nc = c + dc[k];
if (0 <= nr && nr < grid.length &&
0 <= nc && nc < grid[0].length &&
grid[nr][nc] == 1) {
stack.push(new int[]{nr, nc});
grid[nr][nc] = 0;
}
}
}
ans = Math.max(ans, shape);
}
}
}
return ans;
}
}