|
| 1 | +### 827. Making A Large Island |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +题目: |
| 6 | +https://leetcode.com/problems/making-a-large-island/ |
| 7 | + |
| 8 | +难度: |
| 9 | +Hard |
| 10 | + |
| 11 | +题意: |
| 12 | + |
| 13 | +1. 给定一张矩阵图 |
| 14 | +2. 如果两个点相邻且都为1,这两个点连通 |
| 15 | +3. 至多把一个0改为1,问最大的连通岛的面积是多大 |
| 16 | + |
| 17 | +思路: |
| 18 | + |
| 19 | +- 看数据结构,横竖最大是50,整个图最多才2500个点,直接枚举所有的0,改成1之后做dfs判断连通图,都可以解决 |
| 20 | +- 两个连通岛其实是两个集合,如果将一个0改成1,意味着把上下左右所在的岛都合并在一个集合中,我们需要一个数据结构,既可以将集合合并,又可以判断某两个元素是否在同一个集合中,这种数据结构就是并查集 |
| 21 | + |
| 22 | +解法: |
| 23 | + |
| 24 | +```java |
| 25 | +class Solution { |
| 26 | + private class Set { |
| 27 | + |
| 28 | + int[] s; |
| 29 | + int[] num; |
| 30 | + int r; |
| 31 | + int c; |
| 32 | + |
| 33 | + private Set(int r, int c) { |
| 34 | + this.r = r; |
| 35 | + this.c = c; |
| 36 | + this.s = new int[r * c]; |
| 37 | + this.num = new int[r * c]; |
| 38 | + for (int i = 0;i < this.s.length;i++) { |
| 39 | + this.s[i] = i; |
| 40 | + this.num[i] = 1; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private int calIdx(int x, int y) { |
| 45 | + return x * c + y; |
| 46 | + } |
| 47 | + |
| 48 | + private int find(int idx) { |
| 49 | + if (this.s[idx] == idx) { |
| 50 | + return idx; |
| 51 | + } else { |
| 52 | + return this.s[idx] = find(this.s[idx]); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void merge(int x1, int y1, int x2, int y2) { |
| 57 | + int p1 = find(calIdx(x1, y1)); |
| 58 | + int p2 = find(calIdx(x2, y2)); |
| 59 | + if (p1 != p2) { |
| 60 | + this.s[p1] = p2; |
| 61 | + this.num[p2] += this.num[p1]; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + public int largestIsland(int[][] grid) { |
| 68 | + int[] dx = new int[]{0,0,1,-1}; |
| 69 | + int[] dy = new int[]{1,-1,0,0}; |
| 70 | + Set set = new Set(grid.length, grid[0].length); |
| 71 | + for (int i = 0;i < grid.length;i++) { |
| 72 | + for (int j = 0;j < grid[0].length;j++) { |
| 73 | + if (grid[i][j] == 0) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + for (int k = 0;k < 4;k++) { |
| 77 | + if (i + dx[k] < 0 || i + dx[k] >= grid.length) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (j + dy[k] < 0 || j + dy[k] >= grid[0].length) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + if (grid[i + dx[k]][j + dy[k]] == 0) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + set.merge(i, j, i + dx[k], j + dy[k]); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + int max = 0; |
| 92 | + for (int i = 0;i < grid.length;i++) { |
| 93 | + for (int j = 0; j < grid[0].length; j++) { |
| 94 | + if (grid[i][j] == 1) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + HashSet<Integer> t = new HashSet<>(); |
| 99 | + for (int k = 0;k < 4;k++) { |
| 100 | + if (i + dx[k] < 0 || i + dx[k] >= grid.length) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + if (j + dy[k] < 0 || j + dy[k] >= grid[0].length) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + if (grid[i + dx[k]][j + dy[k]] == 0) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + t.add(set.find(set.calIdx(i + dx[k], j + dy[k]))); |
| 111 | + } |
| 112 | + |
| 113 | + int ret = 0; |
| 114 | + for (Integer p: t) { |
| 115 | + ret += set.num[p]; |
| 116 | + } |
| 117 | + ret ++; |
| 118 | + max = Math.max(ret, max); |
| 119 | + } |
| 120 | + } |
| 121 | + if (max == 0) { |
| 122 | + max = grid.length * grid[0].length; |
| 123 | + } |
| 124 | + return max; |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
0 commit comments