Problem description:

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.

1
2
3
4
5
6
7
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.

1
2
3
4
5
6
7
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.

Solution:

  • DFS with memorization
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    class Solution {
    public:
    vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
    int m= grid.size(), n= grid[0].size(), k= hits.size();
    vector<int> res(k);
    //unordered_set<int> noDrop;
    unordered_set<string> noDrop;
    for(int i= 0; i< k; i++) grid[hits[i][0]][hits[i][1]]-= 1;
    for(int i= 0; i< n; i++)
    if(grid[0][i] == 1) check(grid, 0, i, noDrop);

    for(int i= k-1; i>= 0; i--){
    int oldSize= noDrop.size(), x= hits[i][0], y= hits[i][1];
    if(++grid[x][y]!= 1) continue; //was empty-> 0, not falling-> 2

    if( x-1>= 0 && noDrop.count(to_string(x-1)+ ","+to_string(y))
    || x+1< m && noDrop.count(to_string(x+1)+ ","+to_string(y))
    || y-1>= 0 && noDrop.count(to_string(x)+ ","+to_string(y-1))
    || y+1< n && noDrop.count(to_string(x)+ ","+to_string(y+1))
    || x == 0){
    check(grid, x, y, noDrop);
    res[i]= noDrop.size()-oldSize-1;
    }
    }
    return res;
    }

    void check(vector<vector<int>>& grid, int i, int j, unordered_set<string>& noDrop){
    int m= grid.size(), n= grid[0].size();
    string node= to_string(i)+","+to_string(j);
    if(i < 0 || i>= m || j< 0 || j>= n || grid[i][j]!= 1 || noDrop.count(node))
    return;

    noDrop.insert(node);
    check(grid, i-1, j, noDrop);
    check(grid, i+1, j, noDrop);
    check(grid, i, j-1, noDrop);
    check(grid, i, j+1, noDrop);
    }
    };

time complexity: $O()$
space complexity: $O()$
reference: