Problem
Ask
Submissions

Problem: Erect the Fence

Hard
40 min
Explore how to enclose a set of points representing trees on a 2D plane by constructing the shortest possible fence using the convex hull method. Understand the geometric principles behind the problem and develop a solution that returns the fence perimeter coordinates. This lesson helps you apply math and geometry patterns to tackle coding interview questions involving spatial data.

Statement

You are given an array of points, trees, where trees[i] = [xᵢ, yᵢ] represents the location of a tree on a 2D plane. Your goal is to enclose all the trees using the shortest possible length of rope, forming a fence around the garden. A garden is considered well-fenced if every tree lies inside or on the boundary of the fence (i.e., the fence forms the convex hullThis is the smallest convex shape, completely encloses a set of points. of all the points).

Return the coordinates of the trees that lie exactly on the fence perimeter. You can return the answer in any order.

Constraints:

  • 11 \leq trees.length 300\leq 300

  • trees[i].length == 22

  • 00 \leq xi, yi 100\leq 100

  • All the given positions are unique.

Problem
Ask
Submissions

Problem: Erect the Fence

Hard
40 min
Explore how to enclose a set of points representing trees on a 2D plane by constructing the shortest possible fence using the convex hull method. Understand the geometric principles behind the problem and develop a solution that returns the fence perimeter coordinates. This lesson helps you apply math and geometry patterns to tackle coding interview questions involving spatial data.

Statement

You are given an array of points, trees, where trees[i] = [xᵢ, yᵢ] represents the location of a tree on a 2D plane. Your goal is to enclose all the trees using the shortest possible length of rope, forming a fence around the garden. A garden is considered well-fenced if every tree lies inside or on the boundary of the fence (i.e., the fence forms the convex hullThis is the smallest convex shape, completely encloses a set of points. of all the points).

Return the coordinates of the trees that lie exactly on the fence perimeter. You can return the answer in any order.

Constraints:

  • 11 \leq trees.length 300\leq 300

  • trees[i].length == 22

  • 00 \leq xi, yi 100\leq 100

  • All the given positions are unique.