成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 803. Bricks Falling When Hit

YacaToy / 698人閱讀

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];
        }
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72553.html

相關(guān)文章

  • Pygame實戰(zhàn):方塊連接世界,云游大好河山—《我的世界》已上線!確定不進來康康嘛?

    摘要:導(dǎo)語我的世界是一款自由度極高的游戲,每個新存檔的開啟,就像是作為造物主的玩家在虛擬空間開辟了一個全新的宇宙。主題我的世界版本圖片效果圖如下。 導(dǎo)語 《我的世界》是一款自由度極高的游戲,每個新存檔的開啟,就像是作為造物主的玩家在虛擬空間開辟了一個全新的宇宙。 ? 方塊連接世界,云游大好河山。 ...

    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

    摘要:題目鏈接和基本一樣,都可以用,但是大了之后會有很多無效的時間保存在里面,要的話,可能需要遍歷或者用輔助,時間復(fù)雜度超過,所以用一個來做,每次根據(jù)新的改變。 359. Logger Rate Limiter 題目鏈接:https://leetcode.com/problems... 和Design Hit Counter基本一樣,都可以用hashmap,但是timestamp大了之后會有...

    go4it 評論0 收藏0
  • leetcode126. Word Ladder II

    摘要:題目要求相比于,要求返回所有的最短路徑。至于如何生成該有向圖,則需要通過廣度優(yōu)先算法,利用隊列來實現(xiàn)。將每一層的分別入棧。如果遇到則至該層結(jié)尾廣度優(yōu)先算法結(jié)束。通過這種方式來防止形成圈。 題目要求 Given two words (beginWord and endWord), and a dictionarys word list, find all shortest transfo...

    cooxer 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<