Problem solution · Java

Bricks Falling When Hit

Bricks Falling When Hit: a Java solution using disjoint set union. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Disjoint set union
Source
walkccc LeetCode Solutions
Length
101 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Disjoint set union

For Bricks Falling When Hit, the implementation maintains connected components and merges them as relationships are processed.

  1. Give each element a component representative.
  2. Merge representatives when a connection is accepted.
  3. Answer connectivity or component queries from the compressed representatives.

Code notes

  • 101 lines of Java from the credited upstream file 803.java.
  • The implementation visibly relies on sequence storage.
  • 6 loop blocks detected.

Complexity

Account for every find and union operation; with path compression and ranked merging, the amortized cost is nearly constant per operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeBricks Falling When Hit · JavaJava
Use this to learn the idea, then write your own version.
class UnionFind {  public UnionFind(int n) {    id = new int[n];    sz = new int[n];    for (int i = 0; i < n; ++i)      id[i] = i;    Arrays.fill(sz, 1);  }   public void unionBySize(int u, int v) {    final int i = find(u);    final int j = find(v);    if (i == j)      return;    if (sz[i] < sz[j]) {      sz[j] += sz[i];      id[i] = j;    } else {      sz[i] += sz[j];      id[j] = i;    }  }   public int getStableSize() {    // Bricks connected with 0 (top) are stable.    return sz[find(0)];  }   private int[] id;  private int[] sz;   private int find(int u) {    return id[u] == u ? u : (id[u] = find(id[u]));  }}class Solution {  public int[] hitBricks(int[][] grid, int[][] hits) {    this.m = grid.length;    this.n = grid[0].length;     UnionFind uf = new UnionFind(m * n + 1); // 0 := top (stable)     // Mark cells to hit as 2.    for (int[] hit : hits) {      final int i = hit[0];      final int j = hit[1];      if (grid[i][j] == 1)        grid[i][j] = 2;    }     // Union all the 1s.    for (int i = 0; i < m; ++i)      for (int j = 0; j < n; ++j)        if (grid[i][j] == 1)          unionNeighbors(grid, uf, i, j);     int[] ans = new int[hits.length];    int stableSize = uf.getStableSize();     for (int i = hits.length - 1; i >= 0; --i) {      final int x = hits[i][0];      final int y = hits[i][1];      if (grid[x][y] == 2) { // cells marked from 1 to 2        grid[x][y] = 1;      // Unhit and restore it back to 1.        unionNeighbors(grid, uf, x, y);        final int newStableSize = uf.getStableSize();        if (newStableSize > stableSize)          ans[i] = newStableSize - stableSize - 1; // 1 := the hit cell        stableSize = newStableSize;      }    }     return ans;  }   private int m;  private int n;  private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};   private void unionNeighbors(int[][] grid, UnionFind uf, int i, int j) {    final int hash = getHash(i, j);     for (int[] dir : DIRS) {      final int x = i + dir[0];      final int y = j + dir[1];      if (x < 0 || x == m || y < 0 || y == n)        continue;      if (grid[x][y] != 1)        continue;      uf.unionBySize(hash, getHash(x, y));    }     if (i == 0)      uf.unionBySize(hash, 0);  }   private int getHash(int i, int j) {    return i * n + j + 1;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗