Making A Large Island

本文探讨了一种在二维网格中寻找最大岛屿面积的算法。通过使用并查集(UnionFind)结构,文章详细介绍了如何在允许改变一个0为1的前提下,计算最大岛屿的面积。示例展示了不同情况下的解决方案,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In a 2D grid of 0s and 1s, 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 1s).

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值