Maximum Area Rectangle With Point Constraints II - Complete Solution Guide
Maximum Area Rectangle With Point Constraints II is LeetCode problem 3382, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
There are n points on an infinite plane. You are given two integer arrays xCoord and yCoord where (xCoord[i], yCoord[i]) represents the coordinates of the i th point. Your task is to find the maximum area of a rectangle that: Can be formed using four of these points as its corners. Does not contain any other point inside or on its border. Has its edges parallel to the axes. Return the maximum area that you can obtain or -1 if no such rectangle is possible. Example 1: Input: xCoord = [1,1,3,3], y
Detailed Explanation
The problem asks us to find the maximum area of a rectangle formed by four points from a given set of points on a 2D plane, such that the rectangle's sides are parallel to the axes and no other points from the set lie inside or on the border of the rectangle. If no such rectangle exists, we should return -1.
Solution Approach
The solution uses a segment tree with lazy propagation to efficiently find the maximum area rectangle. It first groups points by their y-coordinates. Then, it iterates through the y-coordinates in sorted order. For each y-coordinate, it considers all pairs of x-coordinates on that line. It uses the segment tree to find the highest y-coordinate seen so far between the two x-coordinates. If such a y-coordinate exists, a potential rectangle has been identified, and the area is calculated. After considering all pairs of x-coordinates for a given y, the solution updates the segment tree with the current y-coordinate between each adjacent x-coordinates to mark it as the largest y coordinate up to that point.
Step-by-Step Algorithm
- Step 1: Preprocess the input by extracting unique x-coordinates and creating a mapping from x-coordinate value to its index in the sorted unique x-coordinates list.
- Step 2: Group the x-coordinates based on their corresponding y-coordinates.
- Step 3: Sort the y-coordinates to process them in ascending order.
- Step 4: Initialize a segment tree with lazy propagation. The segment tree is used to store the highest y-coordinate encountered for each range of x-coordinate indices.
- Step 5: Iterate through the sorted y-coordinates.
- Step 6: For each y-coordinate, iterate through the x-coordinates associated with that y-coordinate. Consider pairs of adjacent x-coordinates.
- Step 7: Query the segment tree for the maximum y-coordinate present between the indices of the two x-coordinates.
- Step 8: If a valid y-coordinate is returned from the segment tree (i.e., a rectangle can be formed), calculate the area of the rectangle and update the maximum area if necessary.
- Step 9: After processing all x-coordinate pairs for the current y-coordinate, update the segment tree with the current y-coordinate for the ranges between adjacent x-coordinate indices.
- Step 10: Return the maximum area found, or -1 if no valid rectangle was found.
Key Insights
- Insight 1: The problem can be solved by iterating through pairs of x-coordinates within each horizontal line (same y-coordinate).
- Insight 2: Using a segment tree with lazy propagation allows for efficient querying of the highest y-coordinate between a given range of x-coordinates.
- Insight 3: Sorting the y-coordinates allows us to process the problem in increasing order of y values, enabling the segment tree to track the highest previously encountered y-coordinate for a given x-range.
Complexity Analysis
Time Complexity: O(n*log(n))
Space Complexity: O(n)
Topics
This problem involves: Array, Math, Binary Indexed Tree, Segment Tree, Geometry, Sorting.
Companies
Asked at: UKG.