LeetCode 803. Bricks Falling When Hit 时光倒流 注意标记

231 篇文章 0 订阅
120 篇文章 1 订阅

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.

------------------------------------------------------------

这题时光倒流是关键,还有加一些标记,并查集并不会带来明显的perf上的改善,上codes:

class Solution:
    def hitBricks(self, grid, hits):
        rows,cols = len(grid),len(grid[0]) if grid else 0
        res = []
        if (rows == 0 or cols == 0):
            return 0
        for x,y in hits:
            if (grid[x][y] == 1):#bug3 empty hits use same flg
                grid[x][y] = 3
        def dfs(xx,yy):
            nonlocal rows,cols,grid
            res = 1
            grid[xx][yy] = 2
            for dx,dy in [(0,1),(0,-1),(1,0),(-1,0)]:
                nx,ny = xx+dx,yy+dy
                if (nx>=0 and nx<rows and ny>=0 and ny<cols and grid[nx][ny]==1):
                    res += dfs(nx,ny)
            return res
        def is_connected(xx,yy):
            nonlocal rows,cols,grid
            return any(nx>=0 and nx<rows and ny>=0 and ny<cols and grid[nx][ny]==2
                         for nx,ny in [(xx+1,yy),(xx-1,yy),(xx,yy-1),(xx,yy+1)])
        for j in range(cols):
            if (grid[0][j] == 1):
                dfs(0,j)

        for x,y in hits[::-1]:
            if (grid[x][y] == 3):
                if (is_connected(x,y) or x == 0): #bug1: forget == 0
                    res.append(dfs(x,y)-1)
                else:
                    grid[x][y] = 1  #bug2:
                    res.append(0)
            else:
                res.append(0)
        return res[::-1]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值