Search⌘ K
AI Features

Solution: Erect the Fence

Explore the solution for the Erect the Fence problem by understanding how to compute the convex hull of a set of points. Learn to use the Monotone Chain algorithm with orientation checks to find the minimal fence enclosing all trees. This lesson helps you grasp key geometric concepts and algorithmic steps to implement an efficient convex boundary solution.

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 ...