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:
xCornerandyCornerdefining the top-right corner - Circular obstacles: An array
circles[i] = [xi, yi, ri]where each circle has center(xi, yi)and radiusri
Your mission: Determine if there exists a path from (0, 0) to (xCorner, yCorner) such that:
- The entire path stays inside the rectangle
- The path doesn't touch or enter any circle
- 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
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)
β Quadratic Growth
Space Complexity
O(n)
Space for Union-Find data structure
β‘ Linearithmic Space
Constraints
- 1 β€ xCorner, yCorner β€ 109
- 1 β€ circles.length β€ 1000
- circles[i].length == 3
- 1 β€ xi, yi, ri β€ 109
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code