资讯专栏INFORMATION COLUMN

[LeetCode] 803. Bricks Falling When Hit

YacaToy / 672人阅读

Problem

We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent bricks will not drop.

We will do some erasures sequentially. Each time we want to do the erasure at the location (i, j), the brick (if it exists) on that location will disappear, and then some other bricks may drop because of that erasure.

Return an array representing the number of bricks that will drop after each erasure in sequence.

Example 1:
Input:
grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0]]
Output: [2]
Explanation:
If we erase the brick at (1, 0), the brick at (1, 1) and (1, 2) will drop. So we should return 2.
Example 2:
Input:
grid = [[1,0,0,0],[1,1,0,0]]
hits = [[1,1],[1,0]]
Output: [0,0]
Explanation:
When we erase the brick at (1, 0), the brick at (1, 1) has already disappeared due to the last move. So each erasure will cause no bricks dropping. Note that the erased brick (1, 0) will not be counted as a dropped brick.

Note:

The number of rows and columns in the grid will be in the range [1, 200].
The number of erasures will not exceed the area of the grid.
It is guaranteed that each erasure will be different from any other erasure, and located inside the grid.
An erasure may refer to a location with no brick - if it does, no bricks drop.

reference: https://leetcode.com/problems...

Solution
class Solution {
    public int[] hitBricks(int[][] grid, int[][] hits) {
        int[] res = new int[hits.length];
        int m = grid.length, n = grid[0].length;
        //mark the to-hit bricks to 2
        for (int[] hit: hits) {
            int x = hit[0], y = hit[1];
            if (grid[x][y] == 1) grid[x][y] = 2;
        }
        //m*n for grid elements, 1 for a virtual top "0"
        UnionFind uf = new UnionFind(m*n+1);
        //after bricks hitted, union the ones left
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) unionAround(grid, i, j, uf);
            }
        }
        
        //the count of bricks left
        int count = uf.sizes[uf.find(0)];
        
        for (int i = hits.length-1; i >= 0; i--) {
            int[] hit = hits[i];
            int x = hit[0], y = hit[1];
            if (grid[x][y] == 2) {
                unionAround(grid, x, y, uf);
                grid[x][y] = 1;
            }
            int newSize = uf.sizes[uf.find(0)];
            res[i] = newSize-count > 0 ? newSize-count-1 : 0;
            count = newSize;
        }
        
        return res;
    }
    
    private void unionAround(int[][] grid, int x, int y, UnionFind uf) {
        int m = grid.length, n = grid[0].length;
        int[] dx = new int[]{-1, 1, 0, 0};
        int[] dy = new int[]{0, 0, -1, 1};
        for (int i = 0; i < 4; i++) {
            int nx = x+dx[i];
            int ny = y+dy[i];
            if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
            if (grid[nx][ny] == 1) {
                uf.union(x*n+y+1, nx*n+ny+1);
            }
        }
        if (x == 0) uf.union(x*n+y+1, 0);
    }
}

class UnionFind {
    int[] parents;
    int[] sizes;
    public UnionFind(int n) {
        parents = new int[n];
        sizes = new int[n];
        for (int i = 0; i < n; i++) {
            parents[i] = i;
            sizes[i] = 1;
        }
    }
    public int find(int i) {
        if (parents[i] != i) {
            return find(parents[i]);
        } else {
            return i;
        }
    }
    public void union(int a, int b) {
        int pA = find(a);
        int pB = find(b);
        if (pA != pB) {
            parents[pA] = pB;
            sizes[pB] += sizes[pA];
        }
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72553.html

相关文章

  • Pygame实战:方块连接世界,云游大好河山—《我的世界》已上线!确定不进来康康嘛?

    摘要:导语我的世界是一款自由度极高的游戏,每个新存档的开启,就像是作为造物主的玩家在虚拟空间开辟了一个全新的宇宙。主题我的世界版本图片效果图如下。 导语 《我的世界》是一款自由度极高的游戏,每个新存档的开启,就像是作为造物主的玩家在虚拟空间开辟了一个全新的宇宙。 ​ 方块连接世界,云游大好河山。 ...

    icattlecoder 评论0 收藏0
  • [LeetCode] 819. Most Common Word

    Problem Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isnt banned, and that the...

    SunZhaopeng 评论0 收藏0
  • 359. Logger Rate Limiter & 362. Design Hit Cou

    摘要:题目链接和基本一样,都可以用,但是大了之后会有很多无效的时间保存在里面,要的话,可能需要遍历或者用辅助,时间复杂度超过,所以用一个来做,每次根据新的改变。 359. Logger Rate Limiter 题目链接:https://leetcode.com/problems... 和Design Hit Counter基本一样,都可以用hashmap,但是timestamp大了之后会有...

    go4it 评论0 收藏0
  • leetcode126. Word Ladder II

    摘要:题目要求相比于,要求返回所有的最短路径。至于如何生成该有向图,则需要通过广度优先算法,利用队列来实现。将每一层的分别入栈。如果遇到则至该层结尾广度优先算法结束。通过这种方式来防止形成圈。 题目要求 Given two words (beginWord and endWord), and a dictionarys word list, find all shortest transfo...

    cooxer 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<