[LeetCode] 803. Bricks Falling When Hit 打击砖块掉落
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.
解法:Union Find
Java:
public int[] hitBricks(int[][] grid, int[][] hits) {
int r[] = new int[hits.length], d[] = {-1, 0, 1, 0, -1};
for (int[] h : hits)
grid[h[0]][h[1]] -= 1;
for (int i = 0; i < grid[0].length; i++)
dfs(0, i, grid);
for (int k = hits.length - 1; k >= 0; k--) {
int h[] = hits[k], i = h[0], j = h[1];
grid[i][j] += 1;
if (grid[i][j] == 1 && isConnected(i, j, grid, d))
r[k] = dfs(i, j, grid) - 1;
}
return r;
}
int dfs(int i, int j, int[][] g) {
if (i < 0 || i >= g.length || j < 0 || j >= g[0].length || g[i][j] != 1) return 0;
g[i][j] = 2;
return 1 + dfs(i + 1, j, g) + dfs(i - 1, j, g) + dfs(i, j + 1, g) + dfs(i, j - 1, g);
}
boolean isConnected(int i, int j, int[][] g, int[] d) {
if (i == 0) return true;
for (int k = 1; k < d.length; k++) {
int r = i + d[k - 1], c = j + d[k];
if (0 <= r && r < g.length && 0 <= c && c < g[0].length && g[r][c] == 2)
return true;
}
return false;
}
Python:
class Solution:
def hitBricks(self, grid, hits):
"""
:type grid: List[List[int]]
:type hits: List[List[int]]
:rtype: List[int]
""" m, n = len(grid), len(grid[0]) # Connect unconnected bricks and
def dfs(i, j):
if not (0<=i<m and 0<=j<n) or grid[i][j]!=1:
return 0
ret = 1
grid[i][j] = 2
ret += sum(dfs(x, y) for x, y in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)])
return ret # Check whether (i, j) is connected to Not Falling Bricks
def is_connected(i, j):
return i==0 or any([0<=x<m and 0<=y<n and grid[x][y]==2 for x, y in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]]) # Mark whether there is a brick at the each hit
for i, j in hits:
grid[i][j] -= 1 # Get grid after all hits
for i in range(n):
dfs(0, i) # Reversely add the block of each hits and get count of newly add bricks
ret = [0]*len(hits)
for k in reversed(range(len(hits))):
i, j = hits[k]
grid[i][j] += 1
if grid[i][j]==1 and is_connected(i, j):
ret[k] = dfs(i, j)-1 return ret
Python:
class UnionFind(object):
def __init__(self, n):
self.set = range(n+1)
self.size = [1]*(n+1)
self.size[-1] = 0 def find_set(self, x):
if self.set[x] != x:
self.set[x] = self.find_set(self.set[x]) # path compression.
return self.set[x] def union_set(self, x, y):
x_root, y_root = map(self.find_set, (x, y))
if x_root == y_root:
return False
self.set[min(x_root, y_root)] = max(x_root, y_root)
self.size[max(x_root, y_root)] += self.size[min(x_root, y_root)]
return True def top(self):
return self.size[self.find_set(len(self.size)-1)] class Solution(object):
def hitBricks(self, grid, hits):
"""
:type grid: List[List[int]]
:type hits: List[List[int]]
:rtype: List[int]
"""
def index(C, r, c):
return r*C+c directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
R, C = len(grid), len(grid[0]) hit_grid = [row[:] for row in grid]
for i, j in hits:
hit_grid[i][j] = 0 union_find = UnionFind(R*C)
for r, row in enumerate(hit_grid):
for c, val in enumerate(row):
if not val:
continue
if r == 0:
union_find.union_set(index(C, r, c), R*C)
if r and hit_grid[r-1][c]:
union_find.union_set(index(C, r, c), index(C, r-1, c))
if c and hit_grid[r][c-1]:
union_find.union_set(index(C, r, c), index(C, r, c-1)) result = []
for r, c in reversed(hits):
prev_roof = union_find.top()
if grid[r][c] == 0:
result.append(0)
continue
for d in directions:
nr, nc = (r+d[0], c+d[1])
if 0 <= nr < R and 0 <= nc < C and hit_grid[nr][nc]:
union_find.union_set(index(C, r, c), index(C, nr, nc))
if r == 0:
union_find.union_set(index(C, r, c), R*C)
hit_grid[r][c] = 1
result.append(max(0, union_find.top()-prev_roof-1))
return result[::-1]
C++:
// Time: O(r * c)
// Space: O(r * c) class Solution {
public:
vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
static const vector<pair<int, int>> directions{{-1, 0}, { 1, 0},
{ 0, 1}, { 0, -1}};
const int R = grid.size();
const int C = grid[0].size();
const auto index = [&C](int r, int c) { return r * C + c; }; vector<vector<int>> hit_grid(grid);
for (const auto& hit : hits) {
hit_grid[hit[0]][hit[1]] = 0;
} UnionFind union_find(R * C);
for (int r = 0; r < hit_grid.size(); ++r) {
for (int c = 0; c < hit_grid[r].size(); ++c) {
if (!hit_grid[r][c]) {
continue;
}
if (r == 0) {
union_find.union_set(index(r, c), R * C);
}
if (r && hit_grid[r - 1][c]) {
union_find.union_set(index(r, c), index(r - 1, c));
}
if (c && hit_grid[r][c - 1]) {
union_find.union_set(index(r, c), index(r, c - 1));
}
}
} vector<int> result;
for (int i = hits.size() - 1; i >= 0; --i) {
const auto r = hits[i][0], c = hits[i][1];
const auto prev_roof = union_find.top();
if (grid[r][c] == 0) {
result.emplace_back(0);
continue;
}
for (const auto& d : directions) {
const auto nr = r + d.first, nc = c + d.second;
if (0 <= nr && nr < R &&
0 <= nc && nc < C &&
hit_grid[nr][nc]) {
union_find.union_set(index(r, c), index(nr, nc));
}
}
if (r == 0) {
union_find.union_set(index(r, c), R * C);
}
hit_grid[r][c] = 1;
result.emplace_back(max(0, union_find.top() - prev_roof - 1));
}
reverse(result.begin(), result.end());
return result;
} private:
class UnionFind {
public:
UnionFind(const int n) : set_(n + 1), size_(n + 1, 1) {
iota(set_.begin(), set_.end(), 0);
size_.back() = 0;
} int find_set(const int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
} bool union_set(const int x, const int y) {
int x_root = find_set(x), y_root = find_set(y);
if (x_root == y_root) {
return false;
}
set_[min(x_root, y_root)] = max(x_root, y_root);
size_[max(x_root, y_root)] += size_[min(x_root, y_root)];
return true;
} int top() {
return size_[find_set(size_.size() - 1)];
} private:
vector<int> set_;
vector<int> size_;
};
};
All LeetCode Questions List 题目汇总
[LeetCode] 803. Bricks Falling When Hit 打击砖块掉落的更多相关文章
- [Swift]LeetCode803. 打砖块 | Bricks Falling When Hit
We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only i ...
- [LeetCode] Bricks Falling When Hit 碰撞时砖头掉落
We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only i ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- Java实现 LeetCode 803 打砖块 (DFS)
803. 打砖块 我们有一组包含1和0的网格:其中1表示砖块. 当且仅当一块砖直接连接到网格的顶部,或者它至少有一块相邻(4 个方向之一)砖块不会掉落时,它才不会落下. 我们会依次消除一些砖块.每当我 ...
- LeetCode 931. Minimum Falling Path Sum
原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...
- [LeetCode] 931. Minimum Falling Path Sum 下降路径最小和
Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...
- 【leetcode】699. Falling Squares
题目如下: On an infinite number line (x-axis), we drop given squares in the order they are given. The i- ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- gcc的__builtin_函数(注意前面是两个下划线)
说明: GCC provides a large number of built-in functions other than the ones mentioned above. Some of t ...
- selenium与webdriver驱动与firefox、 chrome匹配版本
一.Chrome python3 selenium3.11.0(直接pip安装即可) chromedriver_win32:2.38 Chrome版本: 65.0.3325.146(正式版本)(32 ...
- luoguP1120小木棍(POJ - 1011 )
题意: 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50,个数不超过65. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段 ...
- 与你一起学习MS Project——基础篇:Project基础应用
为了更清晰容易地熟悉掌握Project的基础应用,我们在基础篇中一起来学习掌握在Project中如何做进度计划.资源计划.成本计划以及跟踪项目的执行情况并生成所需的项目报表. 一.进度计划 这里,首先 ...
- linux下安装Sublime Text3并将它的快捷方式放进启动器中和卸载Sublime
Sublime Text是一个代码编辑器,我主要是用它来编辑python.下面就来简单说明下它在linux的安装过程吧! 1.添加sublime text3的仓库 首先按下快捷键ctrl+alt+t打 ...
- $().on()的知识点
事件冒泡阶段:事件从事件目标(target)开始,往上冒泡直到页面的最上一级标签. 假设一个元素div,它有一个下级元素p.<div> <p>元素</p></ ...
- Switch ……case语句
Switch(变量){ case 1: 如果变量和1的值相同,执行该处代码 break; case 2: 如果变量和2的值相同,执行该处代码 break; case 3: 如果变量和3的值相同,执行该 ...
- CodeChef November Challenge 2019 Division 1题解
传送门 AFO前的最后一场CC了--好好打吧-- \(SIMGAM\) 偶数行的必定两人平分,所以只要抢奇数行中间那个就行了 这题怎么被爆破了 //quming #include<bits/st ...
- Kerberos(一) 安装
1.服务器基本信息 操作系统:centos7 数量:2 主机名映射关系 IP hostname server 192.168.4.50 manager1 Kerberos server(kdc) 19 ...
- 2019 SDN第一次阅读作业
目录 阅读 文章链接 阅读 阅读文章<软件定义网络(SDN)研究进展>,并根据所阅读的文章,书写一篇博客,回答以下问题(至少3个): 1.为什么需要SDN?SDN特点? 背景:传统网络的层 ...