|
39 | 39 | <li><code>0 <= grid[i][j] <= 1</code></li>
|
40 | 40 | </ul>
|
41 | 41 |
|
42 |
| - |
43 | 42 | ## 解法
|
44 | 43 |
|
45 | 44 | <!-- 这里可写通用的实现逻辑 -->
|
46 | 45 |
|
| 46 | +并查集。 |
| 47 | + |
| 48 | +并查集模板: |
| 49 | + |
| 50 | +模板 1——朴素并查集: |
| 51 | + |
| 52 | +```python |
| 53 | +# 初始化,p存储每个点的父节点 |
| 54 | +p = list(range(n)) |
| 55 | + |
| 56 | +# 返回x的祖宗节点 |
| 57 | +def find(x): |
| 58 | + if p[x] != x: |
| 59 | + # 路径压缩 |
| 60 | + p[x] = find(p[x]) |
| 61 | + return p[x] |
| 62 | + |
| 63 | +# 合并a和b所在的两个集合 |
| 64 | +p[find(a)] = find(b) |
| 65 | +``` |
| 66 | + |
| 67 | +模板 2——维护 size 的并查集: |
| 68 | + |
| 69 | +```python |
| 70 | +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 |
| 71 | +p = list(range(n)) |
| 72 | +size = [1] * n |
| 73 | + |
| 74 | +# 返回x的祖宗节点 |
| 75 | +def find(x): |
| 76 | + if p[x] != x: |
| 77 | + # 路径压缩 |
| 78 | + p[x] = find(p[x]) |
| 79 | + return p[x] |
| 80 | + |
| 81 | +# 合并a和b所在的两个集合 |
| 82 | +if find(a) != find(b): |
| 83 | + size[find(b)] += size[find(a)] |
| 84 | + p[find(a)] = find(b) |
| 85 | +``` |
| 86 | + |
| 87 | +模板 3——维护到祖宗节点距离的并查集: |
| 88 | + |
| 89 | +```python |
| 90 | +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 |
| 91 | +p = list(range(n)) |
| 92 | +d = [0] * n |
| 93 | + |
| 94 | +# 返回x的祖宗节点 |
| 95 | +def find(x): |
| 96 | + if p[x] != x: |
| 97 | + t = find(p[x]) |
| 98 | + d[x] += d[p[x]] |
| 99 | + p[x] = t |
| 100 | + return p[x] |
| 101 | + |
| 102 | +# 合并a和b所在的两个集合 |
| 103 | +p[find(a)] = find(b) |
| 104 | +d[find(a)] = distance |
| 105 | +``` |
| 106 | + |
47 | 107 | <!-- tabs:start -->
|
48 | 108 |
|
49 | 109 | ### **Python3**
|
50 | 110 |
|
51 | 111 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
52 | 112 |
|
53 | 113 | ```python
|
| 114 | +class Solution: |
| 115 | + def largestIsland(self, grid: List[List[int]]) -> int: |
| 116 | + n = len(grid) |
| 117 | + p = list(range(n * n)) |
| 118 | + size = [1] * (n * n) |
| 119 | + |
| 120 | + def find(x): |
| 121 | + if p[x] != x: |
| 122 | + p[x] = find(p[x]) |
| 123 | + return p[x] |
54 | 124 |
|
| 125 | + def union(a, b): |
| 126 | + pa, pb = find(a), find(b) |
| 127 | + if pa != pb: |
| 128 | + size[pb] += size[pa] |
| 129 | + p[pa] = pb |
| 130 | + |
| 131 | + def check(i, j): |
| 132 | + return 0 <= i < n and 0 <= j < n and grid[i][j] == 1 |
| 133 | + |
| 134 | + for i in range(n): |
| 135 | + for j in range(n): |
| 136 | + if grid[i][j] == 1: |
| 137 | + for x, y in [[1, 0], [0, 1]]: |
| 138 | + if check(i + x, j +y): |
| 139 | + union(i * n + j, (i + x) * n + j + y) |
| 140 | + |
| 141 | + res = max(size) |
| 142 | + for i in range(n): |
| 143 | + for j in range(n): |
| 144 | + if grid[i][j] == 0: |
| 145 | + t = 1 |
| 146 | + s = set() |
| 147 | + for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]: |
| 148 | + if check(i + x, j + y): |
| 149 | + root = find((i + x) * n + j + y) |
| 150 | + if root not in s: |
| 151 | + t += size[root] |
| 152 | + s.add(root) |
| 153 | + res = max(res, t) |
| 154 | + return res |
55 | 155 | ```
|
56 | 156 |
|
57 | 157 | ### **Java**
|
58 | 158 |
|
59 | 159 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 160 |
|
61 | 161 | ```java
|
| 162 | +class Solution { |
| 163 | + private int n; |
| 164 | + private int[] p; |
| 165 | + private int[] size; |
| 166 | + private int mx; |
| 167 | + private int[][] grid; |
| 168 | + private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; |
| 169 | + |
| 170 | + public int largestIsland(int[][] grid) { |
| 171 | + n = grid.length; |
| 172 | + mx = 1; |
| 173 | + this.grid = grid; |
| 174 | + p = new int[n * n]; |
| 175 | + size = new int[n * n]; |
| 176 | + for (int i = 0; i < p.length; ++i) { |
| 177 | + p[i] = i; |
| 178 | + size[i] = 1; |
| 179 | + } |
| 180 | + for (int i = 0; i < n; ++i) { |
| 181 | + for (int j = 0; j < n; ++j) { |
| 182 | + if (grid[i][j] == 1) { |
| 183 | + for (int[] e : dirs) { |
| 184 | + if (check(i + e[0], j + e[1])) { |
| 185 | + union(i * n + j, (i + e[0]) * n + j + e[1]); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + int res = mx; |
| 192 | + for (int i = 0; i < n; ++i) { |
| 193 | + for (int j = 0; j < n; ++j) { |
| 194 | + if (grid[i][j] == 0) { |
| 195 | + int t = 1; |
| 196 | + Set<Integer> s = new HashSet<>(); |
| 197 | + for (int[] e : dirs) { |
| 198 | + if (check(i + e[0], j + e[1])) { |
| 199 | + int root = find((i + e[0]) * n + j + e[1]); |
| 200 | + if (!s.contains(root)) { |
| 201 | + t += size[root]; |
| 202 | + s.add(root); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + res = Math.max(res, t); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + return res; |
| 211 | + } |
| 212 | + |
| 213 | + private int find(int x) { |
| 214 | + if (p[x] != x) { |
| 215 | + p[x] = find(p[x]); |
| 216 | + } |
| 217 | + return p[x]; |
| 218 | + } |
| 219 | + |
| 220 | + private void union(int a, int b) { |
| 221 | + int pa = find(a), pb = find(b); |
| 222 | + if (pa != pb) { |
| 223 | + size[pb] += size[pa]; |
| 224 | + mx = Math.max(mx, size[pb]); |
| 225 | + p[pa] = pb; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + private boolean check(int i, int j) { |
| 230 | + return i >= 0 && i < n && j >= 0 && j < n && grid[i][j] == 1; |
| 231 | + } |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +### **C++** |
| 236 | + |
| 237 | +```cpp |
| 238 | +class Solution { |
| 239 | +public: |
| 240 | + vector<int> p; |
| 241 | + vector<int> size; |
| 242 | + int n, mx; |
| 243 | + int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; |
| 244 | + |
| 245 | + int largestIsland(vector<vector<int>>& grid) { |
| 246 | + n = grid.size(); |
| 247 | + mx = 1; |
| 248 | + p.resize(n * n); |
| 249 | + size.resize(n * n); |
| 250 | + for (int i = 0; i < p.size(); ++i) |
| 251 | + { |
| 252 | + p[i] = i; |
| 253 | + size[i] = 1; |
| 254 | + } |
| 255 | + for (int i = 0; i < n; ++i) |
| 256 | + { |
| 257 | + for (int j = 0; j < n; ++j) |
| 258 | + { |
| 259 | + if (grid[i][j] == 1) |
| 260 | + { |
| 261 | + for (auto e : dirs) |
| 262 | + { |
| 263 | + if (check(i + e[0], j + e[1], grid)) unite(i * n + j, (i + e[0]) * n + j + e[1]); |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + int res = mx; |
| 269 | + for (int i = 0; i < n; ++i) |
| 270 | + { |
| 271 | + for (int j = 0; j < n; ++j) |
| 272 | + { |
| 273 | + if (grid[i][j] == 0) |
| 274 | + { |
| 275 | + int t = 1; |
| 276 | + unordered_set<int> s; |
| 277 | + for (auto e : dirs) |
| 278 | + { |
| 279 | + if (check(i + e[0], j + e[1], grid)) |
| 280 | + { |
| 281 | + int root = find((i + e[0]) * n + j + e[1]); |
| 282 | + if (!s.count(root)) { |
| 283 | + t += size[root]; |
| 284 | + s.insert(root); |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + res = max(res, t); |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | + return res; |
| 293 | + } |
| 294 | + |
| 295 | + int find(int x) { |
| 296 | + if (p[x] != x) p[x] = find(p[x]); |
| 297 | + return p[x]; |
| 298 | + } |
| 299 | + |
| 300 | + void unite(int a, int b) { |
| 301 | + int pa = find(a), pb = find(b); |
| 302 | + if (pa != pb) |
| 303 | + { |
| 304 | + size[pb] += size[pa]; |
| 305 | + mx = max(mx, size[pb]); |
| 306 | + p[pa] = pb; |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + bool check(int i, int j, vector<vector<int>>& grid) { |
| 311 | + return i >= 0 && i < n && j >= 0 && j < n && grid[i][j] == 1; |
| 312 | + } |
| 313 | +}; |
| 314 | +``` |
| 315 | + |
| 316 | +### **Go** |
| 317 | + |
| 318 | +```go |
| 319 | +var p []int |
| 320 | +var size []int |
| 321 | +var n int |
| 322 | +var mx int |
| 323 | + |
| 324 | +func largestIsland(grid [][]int) int { |
| 325 | + n, mx = len(grid), 1 |
| 326 | + p = make([]int, n*n) |
| 327 | + size = make([]int, n*n) |
| 328 | + for i := 0; i < len(p); i++ { |
| 329 | + p[i] = i |
| 330 | + size[i] = 1 |
| 331 | + } |
| 332 | + |
| 333 | + dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}} |
| 334 | + for i := 0; i < n; i++ { |
| 335 | + for j := 0; j < n; j++ { |
| 336 | + if grid[i][j] == 1 { |
| 337 | + for _, e := range dirs { |
| 338 | + if check(i+e[0], j+e[1], grid) { |
| 339 | + union(i*n+j, (i+e[0])*n+j+e[1]) |
| 340 | + } |
| 341 | + } |
| 342 | + } |
| 343 | + } |
| 344 | + } |
| 345 | + res := mx |
| 346 | + for i := 0; i < n; i++ { |
| 347 | + for j := 0; j < n; j++ { |
| 348 | + if grid[i][j] == 0 { |
| 349 | + t := 1 |
| 350 | + s := make(map[int]bool) |
| 351 | + for _, e := range dirs { |
| 352 | + if check(i+e[0], j+e[1], grid) { |
| 353 | + root := find((i+e[0])*n + j + e[1]) |
| 354 | + if !s[root] { |
| 355 | + t += size[root] |
| 356 | + s[root] = true |
| 357 | + } |
| 358 | + } |
| 359 | + } |
| 360 | + res = max(res, t) |
| 361 | + } |
| 362 | + } |
| 363 | + } |
| 364 | + return res |
| 365 | +} |
| 366 | + |
| 367 | +func find(x int) int { |
| 368 | + if p[x] != x { |
| 369 | + p[x] = find(p[x]) |
| 370 | + } |
| 371 | + return p[x] |
| 372 | +} |
| 373 | + |
| 374 | +func union(a, b int) { |
| 375 | + pa, pb := find(a), find(b) |
| 376 | + if pa != pb { |
| 377 | + size[pb] += size[pa] |
| 378 | + mx = max(mx, size[pb]) |
| 379 | + p[pa] = pb |
| 380 | + } |
| 381 | +} |
| 382 | + |
| 383 | +func check(i, j int, grid [][]int) bool { |
| 384 | + return i >= 0 && i < n && j >= 0 && j < n && grid[i][j] == 1 |
| 385 | +} |
62 | 386 |
|
| 387 | +func max(a, b int) int { |
| 388 | + if a > b { |
| 389 | + return a |
| 390 | + } |
| 391 | + return b |
| 392 | +} |
63 | 393 | ```
|
64 | 394 |
|
65 | 395 | ### **...**
|
|
0 commit comments