博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 803. Bricks Falling When Hit
阅读量:6217 次
发布时间:2019-06-21

本文共 3625 字,大约阅读时间需要 12 分钟。

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:

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

转载地址:http://vblja.baihongyu.com/

你可能感兴趣的文章
LINUX下使用crontab进行RMAN备份实验
查看>>
Hive文件格式
查看>>
[LeetCode] Delete Node in a Linked List
查看>>
农历js脚本
查看>>
获取对象的 RTTI 属性与事件的函数
查看>>
java获得CPU使用率,系统内存,虚拟机内存等情况
查看>>
.NET操作Excel表格的整理
查看>>
vcl.Forms等与VCL界面有关的单元不支持跨平台
查看>>
Windows Vista for Developers——第一部分:Aero向导
查看>>
条件随机场CRF HMM,MEMM的区别
查看>>
CentOS7使用firewalld打开关闭防火墙与端口
查看>>
搜狐首页出现一个硕大的错别字
查看>>
《ASP.NET Web API 2框架揭秘》
查看>>
[导入]竟然支持OpenGL ES!
查看>>
java学习笔记-2
查看>>
csharp 復制DataTable修改某列的值
查看>>
51. 模型动画--动画路径
查看>>
公钥,私钥和数字签名这样最好理解 【转】
查看>>
艾伟_转载:我对NHibernate的感受(1):对延迟加载方式的误解
查看>>
一起谈.NET技术,Visual Studio 2010层架构验证的实现
查看>>