Erect the Fence II Problem
Description
LeetCode Problem 1924.
You are given a 2D integer array trees where trees[i] = [x_i, y_i] represents the location of the i^th tree in the garden.
You are asked to fence the entire garden using the minimum length of rope possible. The garden is well-fenced only if all the trees are enclosed and the rope used forms a perfect circle. A tree is considered enclosed if it is inside or on the border of the circle.
More formally, you must form a circle using the rope with a center (x, y) and radius r where all trees lie inside or on the circle and r is minimum.
Return the center and radius of the circle as a length 3 array [x, y, r]. Answers within 10^(-5) of the actual answer will be accepted.
Example 1:

Input: trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
Output: [2.00000,2.00000,2.00000]
Explanation: The fence will have center = (2, 2) and radius = 2
Example 2:

Input: trees = [[1,2],[2,2],[4,2]]
Output: [2.50000,2.00000,1.50000]
Explanation: The fence will have center = (2.5, 2) and radius = 1.5
Constraints:
1 <= trees.length <= 3000trees[i].length == 20 <= x_i, y_i <= 3000
Solution
Approach: Minimum Enclosing Circle (randomized incremental)
We need the smallest circle containing every point. It is determined by two points (a diameter) or three points on its boundary, and it can be built incrementally:
- Shuffle the points, start with the circle of radius 0 at the first point.
- For each point
inot inside the current circle, the new circle must pass throughi. Restart with the circle{i}. - For each earlier point
jnot inside it, the circle must pass through bothiandj: take the circle with diameterij. - For each earlier point
knot inside that, the circle must pass throughi,jandk: take their circumscribed circle.
The shuffle makes the nested loops cheap in expectation, since point i triggers a rebuild with probability at most 3 / i. A small tolerance (1e-7 on squared distances) absorbs floating-point error, and three collinear or repeated points fall back to the circle on their farthest pair.
Complexity: O(n) expected time and O(1) extra space (beyond the shuffled copy of the points).
import math
class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[float]:
pts = [(float(x), float(y)) for x, y in trees]
n = len(pts)
seed = 12345 # deterministic shuffle: keeps the expected O(n) running time on any input
for i in range(n - 1, 0, -1):
seed = seed * 48271 % 2147483647
j = seed % (i + 1)
pts[i], pts[j] = pts[j], pts[i]
def inside(c, p):
return (p[0] - c[0]) ** 2 + (p[1] - c[1]) ** 2 <= c[2] ** 2 + 1e-7
def two(a, b): # smallest circle through a and b
return ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2, math.hypot(a[0] - b[0], a[1] - b[1]) / 2)
def three(a, b, c): # circle through a, b and c
bx, by, cx, cy = b[0] - a[0], b[1] - a[1], c[0] - a[0], c[1] - a[1]
d = 2 * (bx * cy - by * cx)
if abs(d) < 1e-9: # collinear or repeated points: use the farthest pair
best = max([(a, b), (a, c), (b, c)], key=lambda q: math.hypot(q[0][0] - q[1][0], q[0][1] - q[1][1]))
return two(*best)
b2, c2 = bx * bx + by * by, cx * cx + cy * cy
ux, uy = (cy * b2 - by * c2) / d, (bx * c2 - cx * b2) / d
return (a[0] + ux, a[1] + uy, math.hypot(ux, uy))
circle = (pts[0][0], pts[0][1], 0.0)
for i in range(1, n):
if inside(circle, pts[i]):
continue
circle = (pts[i][0], pts[i][1], 0.0)
for j in range(i):
if inside(circle, pts[j]):
continue
circle = two(pts[i], pts[j])
for k in range(j):
if not inside(circle, pts[k]):
circle = three(pts[i], pts[j], pts[k])
return [circle[0], circle[1], circle[2]]