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 打击砖块掉落的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  4. Java实现 LeetCode 803 打砖块 (DFS)

    803. 打砖块 我们有一组包含1和0的网格:其中1表示砖块. 当且仅当一块砖直接连接到网格的顶部,或者它至少有一块相邻(4 个方向之一)砖块不会掉落时,它才不会落下. 我们会依次消除一些砖块.每当我 ...

  5. LeetCode 931. Minimum Falling Path Sum

    原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...

  6. [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 ...

  7. 【leetcode】699. Falling Squares

    题目如下: On an infinite number line (x-axis), we drop given squares in the order they are given. The i- ...

  8. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. Python 推送RabbitMQ

    username = 'xxxxxxxx' pwd = 'xxxxxxxx' user_pwd = pika.PlainCredentials(username, pwd) s_conn = pika ...

  2. 【云栖社区002-二分估值法】要求不用数学库,求 sqrt (2)精确到小数点后10位(Java版)

    如题 初步审题的时候,想到的是暴力搜索:初步设置一个合法的种子,依次按照1e-2,1e-3,1e-4,1e-5,1e-6 , 1e-7...暴力搜索,额,就是太麻烦了. 打比赛搜索写多了,一看见题目就 ...

  3. python SQLAlchemy的简单配置和查询

    背景: 今天小鱼从0开始配置了下 SQLAlchemy 的连接方式,并查询到了结果,记录下来 需要操作四个地方 1. config  ------数据库地址 2.init ----- 数据库初始化 3 ...

  4. JQuery系列(3) - 工具方法

    jQuery函数库提供了一个jQuery对象(简写为$),这个对象本身是一个构造函数,可以用来生成jQuery对象的实例.有了实例以后,就可以调用许多针对实例的方法,它们定义jQuery.protot ...

  5. SignalR入门一、通过 SignalR 2 进行实时聊天

    一:什么是signalR Asp.net SignalR是微软为实现实时通信的一个类库.一般情况下,signalR会使用JavaScript的长轮询(long polling)的方式来实现客户端和服务 ...

  6. 学习Microsoft Visio(1)

    基础篇 一.认识Visio 1.Visio是什么 Visio最初属于Visio公司,该公司成立于1990年9月.1992年,公司更名为Shapeware.同年11月,它发布了他们公司的第一个产品:Vi ...

  7. 36大数据和about云的文章总结

    36大数据: 白话机器学习 http://www.36dsj.com/archives/78385 基于Hadoop的数据仓库Hive 基础知识(写的很好) http://www.36dsj.com/ ...

  8. java构建简单的HTTP服务器

    使用Java技术构建Web应用时, 我们通常离不开tomcat和jetty之类的servlet容器,这些Web服务器功能强大,性能强劲,深受欢迎,是运行大型Web应用的必备神器. 虽然Java的设计初 ...

  9. es6 promise 所见

    一.Promise是什么? Promise 是异步编程的一种解决方案: 从语法上讲,promise是一个对象,从它可以获取异步操作的消息:从本意上讲,它是承诺,承诺它过一段时间会给你一个结果. pro ...

  10. c++合并两个序列函数merge()和inplace_merge()

    大家在写归并排序时是不是觉得合并两个序列有点麻烦,有快速的方法吗? 我们全部函数自己写,比如: #include<bits/stdc++.h> using namespace std; # ...