|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 | 6 | /**
|
7 |
| - * A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. |
| 7 | + * 305. Number of Islands II |
| 8 | + * |
| 9 | + * A 2d grid map of m rows and n columns is initially filled with water. |
| 10 | + * We may perform an addLand operation which turns the water at position (row, col) into a land. |
| 11 | + * Given a list of positions to operate, count the number of islands after each addLand operation. |
| 12 | + * An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. |
| 13 | + * You may assume all four edges of the grid are all surrounded by water. |
8 | 14 |
|
9 | 15 | Example:
|
10 | 16 |
|
|
41 | 47 | Can you do it in time complexity O(k log mn), where k is the length of the positions?
|
42 | 48 | */
|
43 | 49 | public class _305 {
|
| 50 | + public static class Solution1 { |
44 | 51 |
|
45 |
| - public int find(int[] father, int id) { |
46 |
| - int tf = father[id]; |
47 |
| - while (tf != father[tf]) { |
48 |
| - tf = father[tf]; |
49 |
| - } |
50 |
| - int cur = id; |
51 |
| - int tmp; |
52 |
| - while (father[cur] != tf) { |
53 |
| - tmp = father[cur]; |
54 |
| - father[cur] = tf; |
55 |
| - cur = tmp; |
| 52 | + public int find(int[] father, int id) { |
| 53 | + int tf = father[id]; |
| 54 | + while (tf != father[tf]) { |
| 55 | + tf = father[tf]; |
| 56 | + } |
| 57 | + int cur = id; |
| 58 | + int tmp; |
| 59 | + while (father[cur] != tf) { |
| 60 | + tmp = father[cur]; |
| 61 | + father[cur] = tf; |
| 62 | + cur = tmp; |
| 63 | + } |
| 64 | + return tf; |
56 | 65 | }
|
57 |
| - return tf; |
58 |
| - } |
59 | 66 |
|
60 |
| - public void union(int[] father, int[] sz, int id1, int id2) { |
61 |
| - int tf1 = find(father, id1); |
62 |
| - int tf2 = find(father, id2); |
63 |
| - if (tf1 != tf2) { |
64 |
| - if (sz[tf1] > sz[tf2]) { |
65 |
| - father[tf2] = tf1; |
66 |
| - sz[tf1] += sz[tf2]; |
67 |
| - } else { |
68 |
| - father[tf1] = tf2; |
69 |
| - sz[tf2] += sz[tf1]; |
| 67 | + public void union(int[] father, int[] sz, int id1, int id2) { |
| 68 | + int tf1 = find(father, id1); |
| 69 | + int tf2 = find(father, id2); |
| 70 | + if (tf1 != tf2) { |
| 71 | + if (sz[tf1] > sz[tf2]) { |
| 72 | + father[tf2] = tf1; |
| 73 | + sz[tf1] += sz[tf2]; |
| 74 | + } else { |
| 75 | + father[tf1] = tf2; |
| 76 | + sz[tf2] += sz[tf1]; |
| 77 | + } |
70 | 78 | }
|
71 | 79 | }
|
72 |
| - } |
73 | 80 |
|
74 |
| - public List<Integer> numIslands2(int m, int n, int[][] positions) { |
75 |
| - if (m == 0 || n == 0) { |
76 |
| - return new ArrayList<>(); |
77 |
| - } |
78 |
| - ArrayList<Integer> res = new ArrayList(); |
79 |
| - int[] father = new int[m * n]; |
80 |
| - for (int i = 0; i < father.length; i++) { |
81 |
| - father[i] = -1; |
82 |
| - } |
83 |
| - int[] sz = new int[m * n]; |
84 |
| - int[] dr = { 0, 0, -1, 1 }; |
85 |
| - int[] dc = { -1, 1, 0, 0 }; |
86 |
| - int r; |
87 |
| - int c; |
88 |
| - int nr; |
89 |
| - int nc; |
90 |
| - int count = 0; |
91 |
| - for (int i = 0; i < positions.length; i++) { |
92 |
| - r = positions[i][0]; |
93 |
| - c = positions[i][1]; |
94 |
| - count++; |
95 |
| - father[r * n + c] = r * n + c; |
96 |
| - sz[r * n + c] = 1; |
97 |
| - for (int j = 0; j < 4; j++) { |
98 |
| - nr = r + dr[j]; |
99 |
| - nc = c + dc[j]; |
100 |
| - if (nr >= 0 && nr < m && nc >= 0 && nc < n && father[nr * n + nc] != -1) { |
101 |
| - if (find(father, r * n + c) != find(father, nr * n + nc)) { |
102 |
| - count--; |
103 |
| - union(father, sz, r * n + c, nr * n + nc); |
| 81 | + public List<Integer> numIslands2(int m, int n, int[][] positions) { |
| 82 | + if (m == 0 || n == 0) { |
| 83 | + return new ArrayList<>(); |
| 84 | + } |
| 85 | + ArrayList<Integer> res = new ArrayList(); |
| 86 | + int[] father = new int[m * n]; |
| 87 | + for (int i = 0; i < father.length; i++) { |
| 88 | + father[i] = -1; |
| 89 | + } |
| 90 | + int[] sz = new int[m * n]; |
| 91 | + int[] dr = {0, 0, -1, 1}; |
| 92 | + int[] dc = {-1, 1, 0, 0}; |
| 93 | + int r; |
| 94 | + int c; |
| 95 | + int nr; |
| 96 | + int nc; |
| 97 | + int count = 0; |
| 98 | + for (int i = 0; i < positions.length; i++) { |
| 99 | + r = positions[i][0]; |
| 100 | + c = positions[i][1]; |
| 101 | + count++; |
| 102 | + father[r * n + c] = r * n + c; |
| 103 | + sz[r * n + c] = 1; |
| 104 | + for (int j = 0; j < 4; j++) { |
| 105 | + nr = r + dr[j]; |
| 106 | + nc = c + dc[j]; |
| 107 | + if (nr >= 0 && nr < m && nc >= 0 && nc < n && father[nr * n + nc] != -1) { |
| 108 | + if (find(father, r * n + c) != find(father, nr * n + nc)) { |
| 109 | + count--; |
| 110 | + union(father, sz, r * n + c, nr * n + nc); |
| 111 | + } |
104 | 112 | }
|
105 | 113 | }
|
| 114 | + res.add(count); |
106 | 115 | }
|
107 |
| - res.add(count); |
| 116 | + return res; |
108 | 117 | }
|
109 |
| - return res; |
110 | 118 | }
|
111 |
| - |
112 | 119 | }
|
0 commit comments