Check if the Rectangle Corner Is Reachable - Problem

Imagine you're a robot trying to navigate from the bottom-left corner (0, 0) of a rectangular room to the top-right corner (xCorner, yCorner). However, there are circular obstacles scattered throughout the room that you must avoid!

Given:

  • Rectangle dimensions: xCorner and yCorner defining the top-right corner
  • Circular obstacles: An array circles[i] = [xi, yi, ri] where each circle has center (xi, yi) and radius ri

Your mission: Determine if there exists a path from (0, 0) to (xCorner, yCorner) such that:

  1. The entire path stays inside the rectangle
  2. The path doesn't touch or enter any circle
  3. The path only touches the rectangle at the two corners

Return true if such a path exists, false otherwise. This is essentially asking: "Can you find a safe route through the obstacle course?"

Input & Output

example_1.py β€” Basic Case
$ Input: xCorner = 3, yCorner = 4, circles = [[2,1,1]]
β€Ί Output: true
πŸ’‘ Note: There's a single circle at (2,1) with radius 1. We can find a path from (0,0) to (3,4) by going around the circle. For example, path along the left edge then top edge works.
example_2.py β€” Blocked Path
$ Input: xCorner = 3, yCorner = 3, circles = [[1,1,2]]
β€Ί Output: false
πŸ’‘ Note: The circle at (1,1) with radius 2 is large enough to touch all four boundaries of the 3x3 rectangle, completely blocking any path from (0,0) to (3,3).
example_3.py β€” Multiple Circles
$ Input: xCorner = 3, yCorner = 4, circles = [[2,1,1],[1,2,1]]
β€Ί Output: false
πŸ’‘ Note: Two overlapping circles create a barrier. The first circle (2,1,r=1) and second circle (1,2,r=1) together block all possible paths from bottom-left to top-right.

Visualization

Tap to expand
STARTGOALKey Insight:Instead of finding the actual path,we check if obstacles createa complete barrier!Union-Find Approach:1. Create boundary nodes2. Connect circles to boundaries3. Union overlapping circles4. Check if path is blocked
Understanding the Visualization
1
Setup the Scene
We have a rectangle with circular obstacles inside. The robot starts at (0,0) and wants to reach (xCorner, yCorner).
2
Model as Connectivity
Instead of finding a path, we check if obstacles create an impassable barrier. We create virtual boundary nodes.
3
Connect Components
Use Union-Find to group circles that touch each other and connect them to boundary nodes they touch.
4
Check Blocking
If left boundary connects to right boundary OR top connects to bottom, the path is blocked!
Key Takeaway
🎯 Key Insight: We don't need to find the actual path - just check if circles form a complete barrier connecting opposite boundaries of the rectangle!

Time & Space Complexity

Time Complexity
⏱️
O(nΒ² Γ— Ξ±(n))

Where n is number of circles, Ξ± is inverse Ackermann function (nearly constant)

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

Space for Union-Find data structure

n
2n
⚑ Linearithmic Space

Constraints

  • 1 ≀ xCorner, yCorner ≀ 109
  • 1 ≀ circles.length ≀ 1000
  • circles[i].length == 3
  • 1 ≀ xi, yi, ri ≀ 109
Asked in
Google 28 Meta 22 Amazon 19 Microsoft 15
26.1K Views
Medium Frequency
~35 min Avg. Time
892 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