Skip to main content

Erect the Fence II - Solution & Explanation

HardPremiumFree on FleetCodeArrayMathGeometry4 min readAsked at: Google
Practice this problem

Problem Statement

You are given a 2D integer array trees where trees[i] = [xi, yi] represents the location of the ith 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 <= 3000
  • trees[i].length == 2
  • 0 <= xi, yi <= 3000

Approach Overview

Problem Overview: You are given coordinates of trees on a 2D plane and must build a circular fence that encloses all of them. The goal is to compute the smallest possible circle that contains every point, also known as the minimum enclosing circle.

Approach 1: Brute Force Circle from Pairs and Triplets (O(n^4) time, O(1) space)

The smallest enclosing circle is uniquely determined by either two boundary points (forming the diameter) or three boundary points (forming a circumcircle). A brute force strategy enumerates every pair and triplet of points, constructs the corresponding circle, and checks whether all points lie inside it. For each candidate circle you iterate through the entire point set to verify containment. This produces a worst-case time complexity of O(n^4) for triplet enumeration and validation, with constant auxiliary space. It works conceptually but is far too slow for large inputs.

Approach 2: Reduced Enumeration with Geometric Observations (O(n^3) time, O(1) space)

You can slightly reduce the brute force work by generating circles only from pairs and triplets and validating them immediately. For every pair of points, construct the circle whose diameter is the segment between them. For every triple, compute the circumcircle using standard geometry formulas. After constructing each candidate circle, iterate through all points and ensure their distance from the center is within the radius. This reduces redundant work but still runs in O(n^3) time. It’s mostly useful for understanding the geometric properties behind the optimal solution.

Approach 3: Randomized Welzl’s Algorithm (Expected O(n) time, O(n) space)

The optimal solution uses Welzl’s algorithm, a randomized incremental technique for computing the minimum enclosing circle. Shuffle the points first, then process them one by one. Maintain the current minimal circle. If the next point already lies inside it, continue. If it lies outside, the point must be on the boundary of the final circle. Recompute the circle using that point plus up to two previously processed boundary points. This recursion maintains a small boundary set of size at most three. Because the boundary size is constant and each point is processed once on average, the expected running time is O(n) with O(n) space for recursion and storage.

This algorithm relies heavily on geometric primitives: computing distances, constructing a circle from two points (diameter midpoint), and computing the circumcircle of three points using coordinate geometry. The problem combines concepts from array iteration with computational geometry and numerical reasoning from math.

Recommended for interviews: Randomized Welzl’s algorithm. Interviewers expect you to recognize the minimum enclosing circle pattern and apply the randomized incremental approach. Explaining the brute force pair/triple observation first shows geometric intuition, but implementing Welzl’s algorithm demonstrates strong algorithmic skill and understanding of computational geometry.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force (Pairs + Triplets)O(n^4)O(1)Conceptual understanding of minimum enclosing circle geometry
Optimized EnumerationO(n^3)O(1)Small input sizes or learning circumcircle construction
Randomized Welzl's AlgorithmExpected O(n)O(n)General case and interview‑level optimal solution

Video Solution

Erect the Fence -(GOOGLE) : Explanation ➕ Live CodingcodestorywithMIK5,213 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Erect the Fence II easy or hard?
Erect the Fence II is rated Hard because it requires knowledge of computational geometry and a randomized algorithm. Implementing the circumcircle calculations and maintaining the boundary conditions correctly makes the problem challenging.
Erect the Fence II Python/Java solution
Most implementations follow Welzl’s algorithm. The code shuffles the points, iterates through them, and recursively constructs the minimum circle when a point lies outside the current boundary. This pattern translates cleanly to Python, Java, C++, and Go.
How to solve Erect the Fence II in O(n)?
Shuffle the input points and apply Welzl’s randomized incremental algorithm. Maintain the smallest circle for processed points, and if a new point lies outside it, recompute the circle using that point and up to two boundary points. This yields expected O(n) time.
What is the best approach for Erect the Fence II?
The best approach is Randomized Welzl’s Algorithm for the minimum enclosing circle problem. It incrementally processes points and maintains a boundary set of at most three points that define the circle. The expected time complexity is O(n) with O(n) auxiliary space.
Is Erect the Fence II asked at Google/Amazon/Meta?
Minimum enclosing circle and computational geometry problems appear in interviews at companies like Google and Meta, especially for roles requiring strong algorithmic reasoning. The problem tests geometry, randomized algorithms, and numerical stability.
What data structure is used in Erect the Fence II?
The solution mainly uses arrays to store points and a small boundary set of up to three points. The core logic relies on geometric computations such as distances, midpoints, and circumcircle calculations rather than complex data structures.
What is the time complexity of Erect the Fence II?
The optimal solution using Welzl’s algorithm runs in expected O(n) time because each point is processed once after random shuffling. Brute force approaches that check circles formed by pairs and triples can take O(n^3) to O(n^4) time.

Ready to solve this problem?

Practice Erect the Fence II with our built-in code editor and test cases.

Practice on FleetCode