LeetCode 1924: Erect the Fence II Solution

Master LeetCode problem 1924 (Erect the Fence II), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1924. Erect the Fence II

Problem Explanation

Explanation:

This problem is an extension of the Convex Hull problem, where we are given a set of points with integer coordinates and we need to find the smallest convex polygon that encloses all the points.

The algorithm to solve this problem is the Graham's Scan algorithm, which is a method for finding the convex hull of a finite set of points in the plane. The algorithm uses a stack to build the convex hull incrementally by adding points one by one.

Steps:

  1. Find the point with the lowest y-coordinate (and the leftmost point if there are ties). Let's call this point p0.
  2. Sort the remaining points by polar angle with respect to p0, breaking ties by distance to p0.
  3. Initialize an empty stack to store the convex hull points.
  4. Iterate through the sorted points: a. While the last two points in the stack and the current point do not make a counter-clockwise turn, pop the last point from the stack. b. Push the current point onto the stack.
  5. The stack now contains the points of the convex hull in counterclockwise order.

Finally, we return the points from the stack as the convex hull polygon.

Time complexity: O(n log n) - Sorting the points by polar angle. Space complexity: O(n) - Using a stack to store the convex hull points.

:

Solution Code

class Solution {
    public int[][] outerTrees(int[][] trees) {
        Arrays.sort(trees, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
        Stack<int[]> hull = new Stack<>();
        for (int[] tree : trees) {
            while (hull.size() >= 2 && orientation(hull.get(hull.size() - 2), hull.peek(), tree) < 0) {
                hull.pop();
            }
            hull.push(tree);
        }
        hull.pop();
        for (int i = trees.length - 1; i >= 0; i--) {
            while (hull.size() >= 2 && orientation(hull.get(hull.size() - 2), hull.peek(), trees[i]) < 0) {
                hull.pop();
            }
            hull.push(trees[i]);
        }
        return new ArrayList<>(new HashSet<>(hull)).toArray(new int[0][0]);
    }
    
    private int orientation(int[] p, int[] q, int[] r) {
        return (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1924 (Erect the Fence II)?

This page provides optimized solutions for LeetCode problem 1924 (Erect the Fence II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1924 (Erect the Fence II)?

The time complexity for LeetCode 1924 (Erect the Fence II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1924 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1924 in Java, C++, or Python.

Back to LeetCode Solutions