Erect the Fence II - Problem

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
The Minimum Enclosing Circle Problem๐ŸŒณ Trees to ProtectGoal: Smallest circular fence๐Ÿ“ Mathematical InsightCircle determined by โ‰ค3 boundary points๐ŸŽฏ Algorithm Evolution1. Brute Force: Try all O(nยณ) combinations โ†’ O(nโด) time2. Incremental: Build circle by adding points โ†’ O(nยฒ) time3. Welzl's Magic: Random order + recursive boundary โ†’ O(n) expected!๐Ÿ’ก Key insight: Most random points won't be on the optimal boundary
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!
Asked in
Google 15 Microsoft 8 Amazon 5 Meta 3
28.5K Views
Medium Frequency
~25 min Avg. Time
890 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