You're a landscape architect tasked with creating the most efficient circular fence around a beautiful garden! Given the coordinates of n trees scattered throughout the garden, you need to find the smallest possible circle that can enclose all the trees.
Your fence must be a perfect circle, and every tree must either be inside the circle or exactly on its border. No tree can be left outside! Your goal is to minimize the radius to use the least amount of fencing material.
Input: A 2D array trees where trees[i] = [xi, yi] represents the coordinates of the i-th tree.
Output: Return [x, y, r] where (x, y) is the center of the optimal circle and r is its radius.
Note: Answers within 10-5 of the actual answer will be accepted due to floating-point precision.
Input & Output
example_1.py โ Basic Triangle
$Input:trees = [[1,1],[2,2],[2,0]]
โบOutput:[1.5, 1.0, 1.118033988749895]
๐ก Note:The three trees form a triangle. The minimum enclosing circle passes through all three points, with center at the circumcenter and radius equal to the circumradius.
example_2.py โ Collinear Points
$Input:trees = [[1,2],[2,3],[4,5],[7,8]]
โบOutput:[4.0, 5.0, 4.24264068711928]
๐ก Note:When points are roughly collinear, the optimal circle has diameter connecting the two farthest points. Center is midpoint of the diameter.
example_3.py โ Single Point
$Input:trees = [[0,0]]
โบOutput:[0, 0, 0]
๐ก Note:Edge case: single tree requires no fence (radius = 0), circle center is at the tree location.
Constraints
1 โค trees.length โค 3000
-100 โค xi, yi โค 100
All trees are at distinct locations
Answers within 10-5 of the actual answer will be accepted
Visualization
Tap to expand
Understanding the Visualization
1
Geometric Foundation
Any circle is uniquely determined by at most 3 points on its boundary. This reduces our search space dramatically.
2
Incremental Strategy
Process trees one by one. If a new tree fits inside the current fence, we're good. If not, it must be on the new optimal fence boundary.
3
Randomization Magic
By processing trees in random order, we ensure that the probability of hitting a boundary point decreases exponentially, giving us linear expected time.
4
Recursive Elegance
When we find an outside point, we recursively solve a smaller problem with that point fixed on the boundary.
Key Takeaway
๐ฏ Key Insight: The minimum enclosing circle problem showcases how geometric insights (โค3 boundary points) combined with algorithmic cleverness (randomization) can transform an O(nโด) brute force into an O(n) expected solution. Welzl's algorithm is a masterpiece of computational geometry!
The optimal approach uses Welzl's Algorithm, a randomized incremental algorithm with O(n) expected time complexity. The key insight is that the minimum enclosing circle is determined by at most 3 boundary points, and most randomly ordered points won't force boundary updates.
Common Approaches
Approach
Time
Space
Notes
โ
Brute Force (All Combinations)
O(nโด)
O(1)
Try all possible combinations of 2-3 points to define circles
Welzl's Algorithm (Optimal)
O(n)
O(n)
Randomized incremental algorithm with expected O(n) time complexity
Brute Force (All Combinations) โ Algorithm Steps
Try all single points (degenerate case)
Try all pairs of points (diameter circle)
Try all triplets of points (circumcircle)
For each circle, check if all points are inside
Return the circle with minimum radius
Visualization
Tap to expand
Step-by-Step Walkthrough
1
Test Diameter Circles
For each pair of points, create a circle with them as diameter endpoints
2
Test Circumcircles
For each triplet of points, find the unique circle passing through all three
3
Validate Each Circle
Check if all remaining points lie inside or on the circle boundary
4
Track Minimum
Keep the valid circle with the smallest radius found so far