Convex Hull Boundary Engine 2 — Problem Statement & Solution Guide
Problem Description
Given a set of points in 2D space, find the convex hull boundary using the Heavy-Light Decomposition algorithm.
Examples
Input
[[1, 1], [2, 1], [2, 2], [1, 3]]
Output
[1, 1, 2, 2]
Explanation: Step 1: Sort the points by their polar angles with respect to the point [1, 1]. Step 2: Initialize the convex hull with the first two points [1, 1] and [2, 1]. Step 3: Iterate over the remaining points and add them to the convex hull if they are on the right side of the line formed by the last two points in the hull. Step 4: Return the convex hull.
Input
[[1, 1], [2, 1], [1, 3], [2, 2]]
Output
[1, 1, 2, 1]
Explanation: Step 1: Sort the points by their polar angles with respect to the point [1, 1]. Step 2: Initialize the convex hull with the first two points [1, 1] and [2, 1]. Step 3: Iterate over the remaining points and add them to the convex hull if they are on the right side of the line formed by the last two points in the hull. Step 4: Return the convex hull.
Constraints
- 1 <= N <= 2 * 10^5
- -10^9 <= arr[i] <= 10^9
- Time Complexity: O(N log N) or O(N log^2 N)
- Space Complexity: O(N)
Optimal Approach & Strategy
Use Heavy-Light Decomposition to process subproblems in O(N log N) time and O(N) auxiliary memory.
Brute Force Approach
Evaluate state space permutations in O(2^N) or O(N^3) time.
Verified Code Solutions
function convexHull(points) {
points.sort((a, b) => (a[0] - a[1]) - (b[0] - b[1]));
let hull = [points[0], points[1]];
for (let i = 2; i < points.length; i++) {
while (hull.length > 1 && crossProduct(hull[hull.length - 2], hull[hull.length - 1], points[i]) <= 0) {
hull.pop();
}
hull.push(points[i]);
}
return hull;
}
function crossProduct(p1, p2, p3) {
return (p2[0] - p1[0]) * (p3[1] - p1[1]) - (p2[1] - p1[1]) * (p3[0] - p1[0]);
}class Solution {
public:
int* convexHull(int** points, int pointsSize) {
std::sort(points, points + pointsSize, [](int* a, int* b) {
return (a[0] - a[1]) - (b[0] - b[1]);
});
int* hull = new int[4];
hull[0] = points[0][0];
hull[1] = points[0][1];
hull[2] = points[1][0];
hull[3] = points[1][1];
for (int i = 2; i < pointsSize; i++) {
while (hull[2] && crossProduct(hull[hull[2] - 2], hull[hull[2] - 1], points[i]) <= 0) {
hull[2]--;
}
hull[2]++;
hull[hull[2]] = points[i][0];
hull[hull[2] + 1] = points[i][1];
}
return hull;
}
int crossProduct(int* p1, int* p2, int* p3) {
return (p2[0] - p1[0]) * (p3[1] - p1[1]) - (p2[1] - p1[1]) * (p3[0] - p1[0]);
}
}class Solution {
public int[] convexHull(int[][] points) {
Arrays.sort(points, (a, b) -> (a[0] - a[1]) - (b[0] - b[1]));
int[] hull = new int[2];
hull[0] = points[0][0];
hull[1] = points[0][1];
hull[2] = points[1][0];
hull[3] = points[1][1];
for (int i = 2; i < points.length; i++) {
while (hull.length > 2 && crossProduct(hull[hull.length - 2], hull[hull.length - 1], points[i]) <= 0) {
hull = Arrays.copyOfRange(hull, 0, hull.length - 1);
}
hull = Arrays.copyOf(hull, hull.length + 1);
hull[hull.length - 1] = points[i][0];
hull[hull.length] = points[i][1];
}
return hull;
}
public int crossProduct(int[] p1, int[] p2, int[] p3) {
return (p2[0] - p1[0]) * (p3[1] - p1[1]) - (p2[1] - p1[1]) * (p3[0] - p1[0]);
}
}def convex_hull(points):
points.sort(key=lambda x: (x[0] - x[1], x[0]))
hull = [points[0], points[1]]
for i in range(2, len(points)):
while len(hull) > 1 and cross_product(hull[-2], hull[-1], points[i]) <= 0:
hull.pop()
hull.append(points[i])
return hull
def cross_product(p1, p2, p3):
return (p2[0] - p1[0]) * (p3[1] - p1[1]) - (p2[1] - p1[1]) * (p3[0] - p1[0])function convexHull(points) {
points.sort((a, b) => (a[0] - a[1]) - (b[0] - b[1]));
let hull = [points[0], points[1]];
for (let i = 2; i < points.length; i++) {
while (hull.length > 1 && crossProduct(hull[hull.length - 2], hull[hull.length - 1], points[i]) <= 0) {
hull.pop();
}
hull.push(points[i]);
}
return hull;
}
function crossProduct(p1, p2, p3) {
return (p2[0] - p1[0]) * (p3[1] - p1[1]) - (p2[1] - p1[1]) * (p3[0] - p1[0]);
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.