Erect the Fence II solution and explanation
18 more videos on this problem
Erect the Fence -(GOOGLE) : Explanation ➕ Live CodingcodestorywithMIK · 28:52 · 5K views
Erect the Fence | Leetcode 587 | Live coding session 🔥🔥🔥Coding Decoded · 16:10 · 4K views
Leetcode 587 - Convex Hull | Erect Fence | Advanced AlgorithmNideesh Terapalli · 12:19 · 4K views
LeetCode 587. Erect the FenceHappy Coding · 28:48 · 1K views
587. Erect the Fence - Day 19/30 Leetcode November ChallengeProgramming Live with Larry · 31:48 · 1K views
Leetcode : Erect The Fence (Jarvis Convex Hull)Shivank Goel · 14:53 · 1K views
587 || Erect the Fence || Leetcode Daily ChallengeCodeFreaks · 31:22 · 927 views
587. Erect the Fence - Day 3/30 Leetcode September ChallengeProgramming Live with Larry · 36:12 · 794 views
Leetcode - Erect the Fence (Python)Timothy H Chang · 12:27 · 766 views
LeetCode 587. Erect the Fence - Convex Hull - Monotone Chain - Python 3 - for Coding InterviewsAlice's Adventures in CodeLand · 12:10 · 491 views
Leetcode 587 - Erect the Fence | Explanation | SolutionVijay Bhati · 6:27 · 476 views
Leetcode 587: Erect the FenceExtremeProgrammer · 0:50 · 404 views
#Leetcode 587. Erect the Fence | #JavaScriptLeetcode JavaScript · 12:01 · 229 views
9-Convex hull practice: leetcode_#587.Erect the fence - Brute Force approach(TLE)Think n' Code · 29:07 · 196 views
587. Erect the Fence | JavaScript | LeetCode Daily Challenge | Detailed Explanaiton | ExamplesLeetCodeWithMonu · 22:55 · 195 views
leetcode 587 Erect the Fenceguoguowg · 17:26 · 170 views
11-Convex hull practice: leetcode: #587-Erect the fence Solved using Graham scan algorithmThink n' Code · 17:58 · 51 views
September 3/2021 LeetCode Challenge: Erect the FenceTrong · 12:09 · 24 views
No Sound Leetcode 587: Erect the FenceExtremeProgrammer · 2:45 · 14 views
The problem
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 <= 3000trees[i].length == 20 <= 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.
The written solution for this problem is still being prepared. Try it yourself in the meantime.
Try solving it yourselfComplexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force (Pairs + Triplets) | O(n^4) | O(1) | Conceptual understanding of minimum enclosing circle geometry |
| Optimized Enumeration | O(n^3) | O(1) | Small input sizes or learning circumcircle construction |
| Randomized Welzl's Algorithm | Expected O(n) | O(n) | General case and interview‑level optimal solution |
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.
