Check if the Rectangle Corner Is Reachable - Problem

You are given two positive integers xCorner and yCorner, and a 2D array circles, where circles[i] = [x_i, y_i, r_i] denotes a circle with center at (x_i, y_i) and radius r_i.

There is a rectangle in the coordinate plane with its bottom left corner at the origin (0, 0) and top right corner at the coordinate (xCorner, yCorner).

You need to check whether there is a path from the bottom left corner to the top right corner such that:

  • The entire path lies inside the rectangle
  • Does not touch or lie inside any circle
  • Touches the rectangle only at the two corners

Return true if such a path exists, and false otherwise.

Input & Output

Example 1 — No Blocking Path
$ Input: xCorner = 3, yCorner = 3, circles = [[2,1,1]]
Output: true
💡 Note: Single circle at (2,1) with radius 1 doesn't block the path from (0,0) to (3,3). We can go around it.
Example 2 — Path Completely Blocked
$ Input: xCorner = 3, yCorner = 3, circles = [[1,1,2]]
Output: false
💡 Note: Circle at (1,1) with radius 2 covers both corners (0,0) and (3,3), making any path impossible.
Example 3 — Multiple Small Circles
$ Input: xCorner = 2, yCorner = 2, circles = [[1,0,1],[0,1,1]]
Output: false
💡 Note: Two circles create a barrier: one blocks right movement from (0,0), other blocks upward movement, preventing any path to (2,2).

Constraints

  • 1 ≤ xCorner, yCorner ≤ 109
  • 0 ≤ circles.length ≤ 1000
  • circles[i].length == 3
  • 1 ≤ xi, yi, ri ≤ 109

Visualization

Tap to expand
Rectangle Corner Reachability INPUT (2,1) r=1 Start End 0 1 2 3 xCorner = 3 yCorner = 3 circles = [[2,1,1]] Circle center: (2,1) Radius: 1 ALGORITHM STEPS 1 Init Union-Find Create sets for circles + 4 boundary nodes 2 Check Boundaries Circle touches which rectangle edges? 3 Union Circles Merge overlapping or touching circles 4 Check Blocking Path blocked if boundaries are connected via circles TOP BOTTOM L R Blocking pairs: TOP-LEFT to BOTTOM-RIGHT TOP-RIGHT to BOTTOM-LEFT FINAL RESULT Path exists! true Output Circle doesn't block the path from (0,0) to (3,3) OK Key Insight: Union-Find groups circles that overlap. A path is BLOCKED only if circles form a continuous barrier connecting opposite corners (top-left to bottom-right OR top-right to bottom-left). Here, the single circle at (2,1) doesn't touch opposing boundaries, so path exists! TutorialsPoint - Check if the Rectangle Corner Is Reachable | Union-Find with Boundary Blocking
Asked in
Google 15 Meta 12 Amazon 8
9.0K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen