In a 2D grid of 0
s and 1
s, we change at most one 0
to a 1
.
After, what is the size of the largest island? (An island is a 4-directionally connected group of 1
s).
Example 1:
Input: [[1, 0], [0, 1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
Example 2:
Input: [[1, 1], [1, 0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 4.
Example 3:
Input: [[1, 1], [1, 1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 4.
Notes:
1 <= grid.length = grid[0].length <= 50
.0 <= grid[i][j] <= 1
.
思路:这题跟Number of Islands II,有相似之处。可以结合起来一起看。首先要知道UnionFind不适合减去的情况,但是我们可以用local变量,只算0点变化之后,是否需要加上四周的点的root的size,然后取全局最大,这样UF里面的东西根本不用变化;另外注意,如果四边本身就是相连的(用root判断)那么就不要加第二次;最后如果maxcount如果等于0,表明根本没有0点,直接返回n * n;
T:( n * n). union的步骤接近于constent;
class Solution {
class UnionFind {
public int[] father;
public int[] size;
public UnionFind(int n) {
this.father = new int[n + 1];
this.size = new int[n + 1];
for(int i = 0; i <= n; i++) {
father[i] = i;
size[i] = 1;
}
}
public int find(int x) {
int j = x;
while(father[j] != j) {
j = father[j];
}
// path compression;
while(x != j) {
int fx = father[x];
father[x] = j;
x = fx;
}
return j;
}
public void union(int a, int b) {
int root_a = find(a);
int root_b = find(b);
if(root_a != root_b) {
father[root_a] = root_b;
size[root_b] += size[root_a];
}
}
}
int[][] dirs = new int[][]{{0,1},{0,-1},{-1,0},{1,0}};
public int largestIsland(int[][] grid) {
int n = grid.length;
UnionFind uf = new UnionFind(n * n);
// union existing land;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(grid[i][j] == 1) {
for(int[] dir: dirs) {
int nx = i + dir[0];
int ny = j + dir[1];
if(0 <= nx && nx < n && 0 <= ny && ny < n && grid[nx][ny] == 1) {
uf.union(i * n + j, nx * n + ny);
}
}
}
}
}
// change 0 cell and find maxisland;
int maxisland = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(grid[i][j] == 0) {
HashSet<Integer> rootset = new HashSet<>();
int localcount = 1;
for(int[] dir: dirs) {
int nx = i + dir[0];
int ny = j + dir[1];
if(0 <= nx && nx < n && 0 <= ny && ny < n && grid[nx][ny] == 1) {
int root = uf.find(nx * n + ny);
if(!rootset.contains(root)) {
rootset.add(root);
// 注意这里的uf.size 的index是root,算size永远是用root作为index;
localcount += uf.size[root];
}
}
}
maxisland = Math.max(maxisland, localcount);
}
}
}
return maxisland == 0 ? n * n : maxisland;
}
}