[leetcode] 827. 最大人工岛 | 二维并查集

简介: [leetcode] 827. 最大人工岛 | 二维并查集


class Solution {
private:
    int size[507 * 507];
    int fa[507 * 507];
    void _init() {
        for(int i=0;i<=n * m;i++) fa[i] = i, size[i] = 1;
    }
    int _find(int x) {
        if(fa[x] == x) return x;
        else return fa[x] = _find(fa[x]);
    }
    int maxSize = 1;
    void _union(int u,int v) {
        int fau = _find(u);
        int fav = _find(v);
        if(fau == fav) return ;
        fa[fau] = fav;
        size[fav] += size[fau];
        maxSize = max(maxSize, size[fav]);
    }
    int n, m;
    int get(int x, int y) {
        return x * n + y;
    }
    bool edge(int x, int y) {
        if(x < 0 || x >= n || y < 0 || y >= m) return false;
        return true;
    }
public:
    int largestIsland(vector<vector<int>>& grid) {
        n = grid.size();
        m = grid[0].size();
        _init();
        int dx[4] = {0, 0, -1, 1};
        int dy[4] = {-1, 1, 0, 0};
        for(int i = 0; i < n; i ++) {
            for(int j = 0; j < m; j ++) {
                if(!grid[i][j]) continue;
                for(int to = 0; to < 4; to ++) {
                    int tx = i + dx[to];
                    int ty = j + dy[to];
                    if(edge(tx, ty) && grid[tx][ty]) {
                        _union(get(i,j), get(tx,ty));
                    }
                }
            }
        }
        unordered_map<int,int> mp;
        for(int i = 0;i < n; i ++) {
            for(int j = 0; j < m; j ++) {
                if(grid[i][j]) continue;
                for(int to = 0; to < 4; to ++) {
                    int tx = i + dx[to];
                    int ty = j + dy[to];
                    if(!edge(tx, ty)) continue;
                    if(grid[tx][ty] == 0) continue;
                    int fa = _find(get(tx, ty));
                    mp[fa] = size[fa];
                }
                int total = 1;
                for(unordered_map<int,int> ::iterator it = mp.begin(); it != mp.end(); it ++) {
                    total += it->second;
                }
                //for(auto at: mp) total += at.second;
                maxSize = max(maxSize, total);
                mp.clear();
            }
        }
        return maxSize;
    }
};
目录
相关文章
|
2月前
|
算法
力扣240 搜索二维矩阵II
力扣240 搜索二维矩阵II
|
4月前
|
Go
golang力扣leetcode 240.搜索二维矩阵II
golang力扣leetcode 240.搜索二维矩阵II
19 0
|
4月前
|
算法
【Leetcode 74】搜索二维矩阵 —— 二分查找|矩阵
给你一个满足下述两条属性的`m x n`整数矩阵:每行中的整数从左到右按非严格递增顺序排列。每行的第一个整数大于前一行的最后一个整数
|
4月前
|
JavaScript SoC
leetcode-304:二维区域和检索 - 矩阵不可变
leetcode-304:二维区域和检索 - 矩阵不可变
28 0
|
4月前
|
Go
golang力扣leetcode 74.搜索二维矩阵
golang力扣leetcode 74.搜索二维矩阵
18 0
|
12月前
|
算法 C++
每日算法系列【LeetCode 827】最大人工岛
每日算法系列【LeetCode 827】最大人工岛
|
12月前
|
算法
LeetCode 周赛 347(2023/05/28)二维空间上的 LIS 最长递增子序列问题
这场周赛是 LeetCode 第 347 场单周赛,T4 是结合 LIS 最长递增子序列的动态规划问题。
62 0
|
12月前
|
算法
图解LeetCode——240. 搜索二维矩阵 II
图解LeetCode——240. 搜索二维矩阵 II
5999 0
|
12月前
|
机器学习/深度学习 算法 测试技术
LeetCode:搜索二维矩阵题解
请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征: 每一行的数字都从左到右排序 每一行的第一个数字都比上一行最后一个数字大
106 0
Leetcode动态规划篇(0-1背包问题一维和二维dp实现)
Leetcode动态规划篇(0-1背包问题一维和二维dp实现)

热门文章

最新文章