Problem solution · Java

Check if the Rectangle Corner Is Reachable

Check if the Rectangle Corner Is Reachable: 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
68 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 Check if the Rectangle Corner Is Reachable, 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

  • 68 lines of Java from the credited upstream file 3235.java.
  • The implementation visibly relies on sequence storage.
  • 3 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 codeCheck if the Rectangle Corner Is Reachable · JavaJava
Use this to learn the idea, then write your own version.
class UnionFind {  public UnionFind(int n) {    id = new int[n];    rank = new int[n];    for (int i = 0; i < n; ++i)      id[i] = i;  }   public void unionByRank(int u, int v) {    final int i = find(u);    final int j = find(v);    if (i == j)      return;    if (rank[i] < rank[j]) {      id[i] = j;    } else if (rank[i] > rank[j]) {      id[j] = i;    } else {      id[i] = j;      ++rank[j];    }  }   public int find(int u) {    return id[u] == u ? u : (id[u] = find(id[u]));  }   private int[] id;  private int[] rank;} class Solution {  public boolean canReachCorner(int X, int Y, int[][] circles) {    final int n = circles.length;    // Add two virtual nodes, where node n represents (0, 0) and node n + 1    // represents (X, Y).    UnionFind uf = new UnionFind(n + 2);     // Iterate through each circle.    for (int i = 0; i < n; ++i) {      final int x = circles[i][0];      final int y = circles[i][1];      final int r = circles[i][2];      // Union the current circle with the node (0, 0) if the circle overlaps      // with the left or top edges.      if (x - r <= 0 || y + r >= Y)        uf.unionByRank(i, n);      // Union the current circle with the node (X, Y) if the circle overlaps      // with the right or bottom edges.      if (x + r >= X || y - r <= 0)        uf.unionByRank(i, n + 1);      // Union the current circle with previous circles if they overlap.      for (int j = 0; j < i; j++) {        final int x2 = circles[j][0];        final int y2 = circles[j][1];        final int r2 = circles[j][2];        if ((long) (x - x2) * (x - x2) + (long) (y - y2) * (y - y2) <= (long) (r + r2) * (r + r2))          uf.unionByRank(i, j);      }    }     // If nodes (0, 0) and (X, Y) are in the same union set, that means there's    // a path of overlapping circles that connects the left or top edges to the    // right or bottom edges, implying that (0, 0) cannot reach (X, Y).    return uf.find(n) != uf.find(n + 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 ↗