Imagine you're a city planner looking at a map with building locations marked as points. Your task is to find the smallest rectangular plot of land that can be formed by connecting four of these points, where the rectangle's sides must be parallel to the X and Y axes (no diagonal rectangles allowed!).
Given an array points where points[i] = [xi, yi] represents coordinates on a 2D plane, return the minimum area of any axis-aligned rectangle that can be formed using exactly 4 points from the array. If no such rectangle exists, return 0.
Key insight: A valid rectangle needs exactly 4 points forming two pairs of coordinates - if points (x1,y1), (x1,y2), (x2,y1), and (x2,y2) all exist, they form a rectangle with area |x2-x1| ร |y2-y1|.
Input & Output
example_1.py โ Basic Rectangle
$Input:points = [[1,1],[1,3],[3,1],[3,3]]
โบOutput:4
๐ก Note:These 4 points form a perfect rectangle with corners at (1,1), (1,3), (3,1), and (3,3). The width is |3-1| = 2 and height is |3-1| = 2, giving area = 2 ร 2 = 4.
example_2.py โ Multiple Rectangles
$Input:points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
โบOutput:4
๐ก Note:Even with the extra point (2,2), the minimum rectangle is still formed by the same 4 corner points, giving area 4. The point (2,2) doesn't participate in forming any smaller rectangles.
example_3.py โ No Rectangle Possible
$Input:points = [[1,1],[1,3],[3,1]]
โบOutput:0
๐ก Note:With only 3 points, it's impossible to form a rectangle (which requires exactly 4 points). Even if we had 4+ points, they would need to form two pairs of coordinates to make a valid axis-aligned rectangle.
Constraints
1 โค points.length โค 500
points[i].length == 2
0 โค xi, yi โค 4 ร 104
All points are unique
Rectangle sides must be parallel to X and Y axes
Visualization
Tap to expand
Understanding the Visualization
1
Identify Diagonal Strategy
Any rectangle is uniquely determined by its diagonal corners
2
Store Points Efficiently
Use hash set for instant point existence checking
3
Test Diagonal Pairs
For each pair, calculate where other corners must be
4
Validate & Calculate
Check if calculated corners exist, compute area
5
Track Minimum
Keep track of the smallest area found
Key Takeaway
๐ฏ Key Insight: Instead of checking all 4-point combinations (O(nโด)), focus on diagonal pairs - if two points can be diagonal corners, the other two positions are mathematically determined and can be verified in O(1) time using a hash set!
The optimal approach uses a two-pass hash table strategy: first store all points for O(1) lookup, then examine each pair of points as potential diagonal corners. For each diagonal pair with different x and y coordinates, verify that the other two calculated corner points exist in the hash set. This reduces complexity from O(nโด) brute force to O(nยฒ) time with O(n) space.
Common Approaches
Approach
Time
Space
Notes
โ
Two-Pass Hash Table (Diagonal Approach)
O(nยฒ)
O(n)
First store all points in hash set, then check each diagonal pair for valid rectangles
Brute Force (Check All 4-Point Combinations)
O(nโด)
O(1)
Try every possible combination of 4 points to see if they form a rectangle