Erect the Fence II — LeetCode 1924 Python Solution

HardLeetCode PremiumGeometryArrayMath
Problem
#1924
Reading time
2 min

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.

Example

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

Complexity

MeasureComplexity
TimeO(n) or O(1)
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1924. Erect the Fence II is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math and Geometry.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1924. Erect the Fence II?
LeetCode 1924. Erect the Fence II is rated Hard on LeetCode.
What topics does LeetCode 1924. Erect the Fence II cover?
LeetCode 1924. Erect the Fence II is tagged Geometry, Array and Math on LeetCode.
Is LeetCode 1924. Erect the Fence II a premium problem?
Yes. LeetCode 1924. Erect the Fence II is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview