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...
Solutionclass 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]; } } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72553.html
摘要:導語我的世界是一款自由度極高的游戲,每個新存檔的開啟,就像是作為造物主的玩家在虛擬空間開辟了一個全新的宇宙。主題我的世界版本圖片效果圖如下。 導語 《我的世界》是一款自由度極高的游戲,每個新存檔的開啟,就像是作為造物主的玩家在虛擬空間開辟了一個全新的宇宙。 ? 方塊連接世界,云游大好河山。 ...
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...
摘要:題目鏈接和基本一樣,都可以用,但是大了之后會有很多無效的時間保存在里面,要的話,可能需要遍歷或者用輔助,時間復雜度超過,所以用一個來做,每次根據新的改變。 359. Logger Rate Limiter 題目鏈接:https://leetcode.com/problems... 和Design Hit Counter基本一樣,都可以用hashmap,但是timestamp大了之后會有...
摘要:題目要求相比于,要求返回所有的最短路徑。至于如何生成該有向圖,則需要通過廣度優先算法,利用隊列來實現。將每一層的分別入棧。如果遇到則至該層結尾廣度優先算法結束。通過這種方式來防止形成圈。 題目要求 Given two words (beginWord and endWord), and a dictionarys word list, find all shortest transfo...
閱讀 2878·2021-10-14 09:50
閱讀 1226·2021-10-08 10:21
閱讀 3659·2021-10-08 10:16
閱讀 3067·2021-09-27 14:02
閱讀 3142·2021-09-23 11:21
閱讀 2126·2021-09-07 10:17
閱讀 413·2019-08-30 14:00
閱讀 2115·2019-08-29 17:26