Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Erect the Fence

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "587" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given an array trees where trees[i] = [xi, yi] represents the location of a tree in the garden. Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if all the trees are enclosed. Return the coordinates of trees that are exactly located on the fence perimeter. You may return the answer in any order. Example 1: Input: trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: [[1,1],[2,0],[4,2],[3,3],[2,4]] Explanation: All the trees will be on the perimeter of the fence except the tree at [2, 2], which will be inside the fence. Example 2: Input: trees = [[1,2],[2,2],[4,2]] Output: [[4,2],[2,2],[1,2]] Explanation: The fence forms a line that passes through all the trees. Constraints: 1 <= trees.length <= 3000 trees[i].length == 2 0 <= xi, yi <= 100 All the given positions are unique.

Explanation

Here's a breakdown of the solution:

  • Convex Hull: The problem is essentially asking for the convex hull of a set of points (trees). The convex hull is the smallest convex polygon that encloses all the points.
  • Graham Scan: We use the Graham scan algorithm, a standard and efficient algorithm for finding the convex hull. It involves sorting the points and then iterating through them twice to build the upper and lower hulls.
  • Orientation Test: A crucial part of the algorithm is determining the orientation of three points. This tells us whether a point is to the left, right, or collinear with a line formed by the other two points. We maintain the hull such that all turns are counter-clockwise (or clockwise, depending on the implementation).

  • Runtime Complexity: O(n log n) due to the sorting step. Storage Complexity: O(n) to store the convex hull.

Code

    def outerTrees(trees):
    """
    Finds the coordinates of trees on the fence perimeter (convex hull).

    Args:
        trees: A list of tree coordinates [[x1, y1], [x2, y2], ...].

    Returns:
        A list of tree coordinates that form the convex hull.
    """

    def orientation(p, q, r):
        """
        Determines the orientation of three points (p, q, r).
        Returns:
            0 if collinear
            1 if clockwise
            2 if counterclockwise
        """
        val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
        if val == 0:
            return 0  # Collinear
        return 1 if val > 0 else 2  # Clockwise or Counterclockwise

    # Sort the trees by x-coordinate (and then y-coordinate if x is the same)
    trees.sort(key=lambda p: (p[0], p[1]))

    n = len(trees)
    if n <= 3:
        return trees  # A line or a triangle

    # Build the lower hull
    lower_hull = []
    for p in trees:
        while len(lower_hull) >= 2 and orientation(lower_hull[-2], lower_hull[-1], p) != 2:
            lower_hull.pop()
        lower_hull.append(p)

    # Build the upper hull
    upper_hull = []
    for p in reversed(trees):
        while len(upper_hull) >= 2 and orientation(upper_hull[-2], upper_hull[-1], p) != 2:
            upper_hull.pop()
        upper_hull.append(p)

    # Combine the hulls, removing duplicates
    convex_hull = lower_hull[:-1] + upper_hull[:-1]  # Remove first and last points of each hull
    convex_hull = list(dict.fromkeys(convex_hull)) # Remove duplicates while preserving order


    return convex_hull

More from this blog

C

Chatmagic blog

2894 posts