Problem
You are given an array of points in the X-Y plane points
where
points[i] = [xi, yi]
.
Return the minimum area of any rectangle formed from these points, with sidesnot necessarily parallel to the X and Y axes. If there is not any such rectangle, return 0
.
Answers within 10-5
of the actual answer will be accepted.
Examples
Example 1
|
|
Example 2
|
|
Example 3
|
|
Constraints
1 <= points.length <= 50
points[i].length == 2
0 <= xi, yi <= 4 * 10^4
- All the given points are unique.
Solution
Method 1 – Hash Set + Geometry
Intuition
To find the minimum area rectangle (not necessarily axis-aligned), we need to check all quadruples of points that can form a rectangle. For any two points, if their midpoint and distance are the same as another pair, they can be diagonally opposite corners of a rectangle. We use a hash map to group pairs by their midpoint and distance, then check for rectangles.
Approach
- For each pair of points, compute their midpoint and squared distance.
- Group pairs by (midpoint, distance) in a hash map.
- For each group, for every pair of pairs, compute the area using the distance between one pair and the other.
- Track the minimum area found.
- Return the minimum area, or 0 if no rectangle is found.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n^3)
- Enumerating all pairs and checking rectangles.
- 🧺 Space complexity:
O(n^2)
- For storing pairs in the hash map.