695 Max Area of Island

1. Question

Given a non-empty 2D arraygridof 0's and 1's, an island is a group of1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)
Example 1:
1
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
2
[0,0,0,0,0,0,0,1,1,1,0,0,0],
3
[0,1,1,0,1,0,0,0,0,0,0,0,0],
4
[0,1,0,0,1,1,0,0,1,0,1,0,0],
5
[0,1,0,0,1,1,0,0,1,1,1,0,0],
6
[0,0,0,0,0,0,0,0,0,0,1,0,0],
7
[0,0,0,0,0,0,0,1,1,1,0,0,0],
8
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Copied!
Given the above grid, return6. Note the answer is not 11, because the island must be connected 4-directionally.
Example 2:
1
[[0,0,0,0,0,0,0,0]]
Copied!
Given the above grid, return0.
Note:The length of each dimension in the givengriddoes not exceed 50.

2. Implementation

(1) DFS
1
class Solution {
2
public int maxAreaOfIsland(int[][] grid) {
3
if (grid == null || grid.length == 0) {
4
return 0;
5
}
6
7
int m = grid.length, n = grid[0].length;
8
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
9
int maxArea = 0, area = 0;
10
11
for (int i = 0; i < m; i++) {
12
for (int j = 0; j < n; j++) {
13
if (grid[i][j] == 1) {
14
area = getIslandArea(i, j, grid, directions);
15
maxArea = Math.max(maxArea, area);
16
}
17
}
18
}
19
return maxArea;
20
}
21
22
public int getIslandArea(int row, int col, int[][] grid, int[][] directions) {
23
int area = 1;
24
grid[row][col] = 0;
25
26
for (int[] direction : directions) {
27
int nextRow = row + direction[0];
28
int nextCol = col + direction[1];
29
30
if (isValid(nextRow, nextCol, grid, directions)) {
31
area += getIslandArea(nextRow, nextCol, grid, directions);
32
}
33
}
34
return area;
35
}
36
37
public boolean isValid(int row, int col, int[][] grid, int[][] directions) {
38
return row >= 0 && row < grid.length && col >= 0 && col < grid[0].length && grid[row][col] == 1;
39
}
40
}
Copied!
(2) Union Find
1
class Solution {
2
public int maxAreaOfIsland(int[][] grid) {
3
if (grid == null || grid.length == 0) {
4
return 0;
5
}
6
7
int m = grid.length, n = grid[0].length;
8
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
9
10
UnionFind uf = new UnionFind(grid);
11
12
for (int i = 0; i < m; i++) {
13
for (int j = 0; j < n; j++) {
14
if (grid[i][j] == 1) {
15
for (int[] direction : directions) {
16
int nextRow = i + direction[0];
17
int nextCol = j + direction[1];
18
19
if (isValid(nextRow, nextCol, grid)) {
20
int id1 = i * n + j;
21
int id2 = nextRow * n + nextCol;
22
uf.union(id1, id2);
23
}
24
}
25
}
26
}
27
}
28
return uf.maxArea();
29
}
30
31
public boolean isValid(int row, int col, int[][] grid) {
32
return row >= 0 && row < grid.length && col >= 0 && col < grid[0].length && grid[row][col] == 1;
33
}
34
35
class UnionFind {
36
int[] sets;
37
int[] size;
38
int m, n, count, maxSize;
39
40
public UnionFind(int[][] grid) {
41
m = grid.length;
42
n = grid[0].length;
43
sets = new int[m * n];
44
size = new int[m * n];
45
maxSize = 0;
46
47
for (int i = 0; i < m; i++) {
48
for (int j = 0; j < n; j++) {
49
if (grid[i][j] == 1) {
50
int id = i * n + j;
51
sets[id] = id;
52
size[id] = 1;
53
maxSize = 1;
54
++count;
55
}
56
}
57
}
58
}
59
60
public int find(int node) {
61
while (node != sets[node]) {
62
node = sets[node];
63
}
64
return node;
65
}
66
67
public void union(int i, int j) {
68
int node1 = find(i);
69
int node2 = find(j);
70
71
if (node1 == node2) {
72
return;
73
}
74
75
if (size[node1] < size[node2]) {
76
sets[node1] = node2;
77
size[node2] += size[node1];
78
maxSize = Math.max(maxSize, size[node2]);
79
}
80
else {
81
sets[node2] = node1;
82
size[node1] += size[node2];
83
maxSize = Math.max(maxSize, size[node1]);
84
}
85
86
--count;
87
}
88
89
public int maxArea() {
90
return maxSize;
91
}
92
}
93
}
Copied!

3. Time & Space Complexity

DFS: 时间复杂度O(mn), 空间复杂度O(max(m,n))
Union Find: 时间复杂度O(mn * log(mn)), 空间复杂度O(mn)