算法LeetCode

803. 打砖块

Desp:
有一个 m x n 的二元网格 grid ,其中 1 表示砖块,0 表示空白。砖块 稳定(不会掉落)的前提是: 一块砖直接连接到网格的顶部,或者 至少有一块相邻(4 个方向之一)砖块 稳定 不会掉落时 给你一个数组 hits ,这是需要依次消除砖块的位置。每当消除 hits[i] = (rowi, coli) 位置上的砖块时,对应位置的砖块(若存在)会消失,然后其他的砖块可能因为这一消除操作而 掉落 。一旦砖块掉落,它会 立即 从网格 grid 中消失(即,它不会落在其他稳定的砖块上)。 返回一个数组 result ,其中 result[i] 表示第 i 次消除操作对应掉落的砖块数目。

注意,消除可能指向是没有砖块的空白位置,如果发生这种情况,则没有砖块掉落。

class Solution {
    public int[] hitBricks(int[][] grid, int[][] hits) {
        
        int n = hits.length;
        int[] ans = new int[n];
        
        for(int[] h : hits) {
            int r = h[0];
            int c = h[1];
            if(grid[r][c] == 1) {
                grid[r][c] = 2;
            }
            
        }
        UnionFind uf = new UnionFind(grid);
        
        for(int i = n - 1; i >= 0; i--) {
            int r = hits[i][0];
            int c = hits[i][1];
            if(grid[r][c] == 2) {
                ans[i] = uf.manager(r, c);
            }
        }
        return ans;
    }
}
class UnionFind {
    int[][] grid;//修改过的数组
    int n;
    int m;
    int cellingAll;//连在天花板砖块的数量
    Dot[][] dots;//点
    HashSet<Dot> cellingSet;//天花板的点
    HashMap<Dot, Dot> fatherMap;
    HashMap<Dot, Integer> sizeMap;
    public UnionFind(int[][] matrix) {
        inti(matrix);
        connect();
    }

    public int manager(int i, int j) {
        grid[i][j] = 1;
        Dot cur = new Dot();
        dots[i][j] = cur;
        if(i == 0) {
            cellingAll++;
            cellingSet.add(cur);
        }
        fatherMap.put(cur, cur);
        sizeMap.put(cur, 1);
        int pre = cellingAll;
        union(i, j, i - 1, j);
        union(i, j, i + 1, j);
        union(i, j, i, j - 1);
        union(i, j, i, j + 1);
        int now = cellingAll;
        if(i == 0) {
            return now - pre;
        }
        return now == pre ? 0 : now - pre - 1;
    }
    private void union(int r1, int c1, int r2, int c2) {
        if(valid(r1, c1) && valid(r2, c2)) {
            Dot f1 = find(r1, c1);
            Dot f2 = find(r2, c2);
            if(f1 != f2) {
                int size1 = sizeMap.get(f1);
                int size2 = sizeMap.get(f2);
                boolean statu1 = cellingSet.contains(f1);
                boolean statu2 = cellingSet.contains(f2);
                if(size1 < size2) {
                    fatherMap.put(f1, f2);
                    sizeMap.put(f2, size1 + size2);
                    if(statu1 ^ statu2) {
                        cellingSet.add(f2);
                        cellingAll += statu1 ? size2 : size1;
                    }
                } else {
                    fatherMap.put(f2, f1);
                    sizeMap.put(f1, size1 + size2);
                    if(statu1 ^ statu2) {
                        cellingSet.add(f1);
                        cellingAll += statu1 ? size2 : size1;
                    }
                }
            }
        }
    }

    private Dot find(int r, int c) {
        Dot dot = dots[r][c];
        Stack<Dot> stack = new Stack<>();
        while(dot != fatherMap.get(dot)) {
            stack.push(dot);
            dot = fatherMap.get(dot);
        }
        while(!stack.isEmpty()) {
            fatherMap.put(stack.pop(), dot);
        }
        return dot;
    }
    private boolean valid(int i, int j) {
        return i < n && i >= 0 && j < m && j >= 0 && grid[i][j] == 1;
    }
    private void connect() {
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++ ) {
                union(i, j, i - 1, j);
                union(i, j, i + 1, j);
                union(i, j, i, j - 1);
                union(i, j, i, j + 1);
            }
        }
    }
    private void inti(int[][] matrix) {
        grid = matrix;
        n = matrix.length;
        m = matrix[0].length;
        cellingAll = 0;
        cellingSet = new HashSet<>();
        fatherMap = new HashMap<>();
        sizeMap = new HashMap<>();
        dots = new Dot[n][m];
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++ ){
                if(grid[i][j] == 1) {
                    Dot cur = new Dot();
                    dots[i][j] = cur;
                    fatherMap.put(cur, cur);
                    sizeMap.put(cur, 1);
                    if(i == 0) {
                        cellingAll++;
                        cellingSet.add(cur);
                    }
                }
            }
        }
    }
}
class Dot {}

本文由 mdnice 多平台发布

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值