Maximum Area Rectangle With Point Constraints I - Problem
You're an architect designing a rectangular foundation on a plot of land with existing structures. Given an array
๐น The rectangle uses exactly four of these points as its corners
๐น The rectangle contains no other points inside or on its borders
๐น The rectangle's edges are parallel to the coordinate axes
Return the maximum area achievable, or
points where points[i] = [xi, yi] represents the coordinates of structures on an infinite plane, your task is to find the maximum area rectangle that can be built such that:๐น The rectangle uses exactly four of these points as its corners
๐น The rectangle contains no other points inside or on its borders
๐น The rectangle's edges are parallel to the coordinate axes
Return the maximum area achievable, or
-1 if no valid rectangle exists. This problem tests your ability to work with computational geometry, coordinate validation, and optimization. Input & Output
example_1.py โ Basic Rectangle
$
Input:
points = [[1,1], [1,3], [3,1], [3,3]]
โบ
Output:
4
๐ก Note:
The four points form a perfect 2ร2 rectangle with no other points inside. The corners are at (1,1), (1,3), (3,1), and (3,3), giving us an area of (3-1) ร (3-1) = 4.
example_2.py โ Point Inside Rectangle
$
Input:
points = [[1,1], [1,3], [3,1], [3,3], [2,2]]
โบ
Output:
-1
๐ก Note:
Although points (1,1), (1,3), (3,1), (3,3) could form a rectangle, there's a point at (2,2) inside it. Since rectangles cannot contain other points, no valid rectangle exists.
example_3.py โ Multiple Valid Rectangles
$
Input:
points = [[1,1], [1,3], [3,1], [3,3], [1,5], [3,5]]
โบ
Output:
4
๐ก Note:
Two valid rectangles exist: (1,1)โ(3,3) with area 4, and (1,3)โ(3,5) with area 4. We return the maximum area, which is 4.
Visualization
Tap to expand
Understanding the Visualization
1
Diagonal Recognition
Two points form a valid diagonal if they have different x AND y coordinates
2
Corner Calculation
For diagonal (x1,y1)โ(x2,y2), other corners are (x1,y2) and (x2,y1)
3
Existence Verification
Use hash table to quickly check if calculated corners exist in point set
4
Boundary Validation
Ensure no other points lie inside or on the rectangle's boundary
Key Takeaway
๐ฏ Key Insight: By recognizing that any rectangle is uniquely defined by its diagonal, we can reduce the problem from checking O(nโด) combinations to O(nยฒ) diagonal pairs, making the solution much more efficient.
Time & Space Complexity
Time Complexity
O(nโด)
Four nested loops to check all combinations of 4 points, where n is the number of points
โ Linear Growth
Space Complexity
O(1)
Only using constant extra space for variables and calculations
โ Linear Space
Constraints
- 1 โค points.length โค 50
- points[i].length == 2
- 0 โค xi, yi โค 5 ร 104
- All given points are unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code