LeetCode Problem Workspace
Minimum Area Rectangle II
Find the minimum area rectangle from given points in the X-Y plane, with sides not necessarily parallel to the axes.
4
Topics
5
Code langs
3
Related
Practice Focus
Medium · Array scanning plus hash lookup
Answer-first summary
Find the minimum area rectangle from given points in the X-Y plane, with sides not necessarily parallel to the axes.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array scanning plus hash lookup
To solve the Minimum Area Rectangle II problem, use a combination of array scanning and hash lookup. You can find all pairs of points that could form a potential rectangle, and then verify the area of the rectangle formed. The answer requires efficient checking and validation of the geometry conditions with hash tables to reduce time complexity.
Problem Statement
You are given an array of points in the X-Y plane, where each point is represented as [xi, yi]. Your task is to return the minimum area of any rectangle that can be formed by these points. The rectangle does not have to have its sides parallel to the X and Y axes.
If it is impossible to form a rectangle from the points, return 0. Ensure that the area is calculated as a floating-point number, with answers within 10^-5 of the actual value accepted.
Examples
Example 1
Input: points = [[1,2],[2,1],[1,0],[0,1]]
Output: 2.00000
The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.
Example 2
Input: points = [[0,1],[2,1],[1,1],[1,0],[2,0]]
Output: 1.00000
The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.
Example 3
Input: points = [[0,3],[1,2],[3,1],[1,3],[2,1]]
Output: 0
There is no possible rectangle to form from these points.
Constraints
- 1 <= points.length <= 50
- points[i].length == 2
- 0 <= xi, yi <= 4 * 104
- All the given points are unique.
Solution Approach
Array Scanning
Begin by scanning all pairs of points in the array. For each pair, check if a rectangle can be formed by finding the other two points that would complete the rectangle. This process involves verifying that the points are aligned diagonally and meet the conditions for forming a rectangle.
Hash Table Lookup
Use a hash table to store the points efficiently. This allows you to quickly check if a point exists while scanning pairs. This reduces the number of comparisons and speeds up the process of verifying possible rectangles.
Area Calculation
For each valid rectangle, calculate the area by determining the distance between opposite corners of the rectangle. Keep track of the smallest area encountered and return it as the result. If no rectangle is found, return 0.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
The time complexity depends on the approach used for scanning the array and performing hash lookups. In the worst case, it can be O(n^2), where n is the number of points. The space complexity is O(n) due to the use of a hash table to store the points.
What Interviewers Usually Probe
- The candidate should be able to efficiently identify pairs of points that could form a rectangle.
- Expect the candidate to consider and explain how hash tables can optimize the solution's time complexity.
- Look for the candidate's ability to compute the area correctly and return the smallest possible value.
Common Pitfalls or Variants
Common pitfalls
- Candidates may forget to check that the points form a valid rectangle by comparing the diagonal points.
- Some might overlook the importance of hashing for efficient lookup and resort to inefficient brute force methods.
- There might be confusion regarding the precision required for floating-point answers.
Follow-up variants
- Extend this problem by considering additional constraints such as large point sets or varying dimensions.
- Modify the problem to find the largest possible rectangle instead of the smallest.
- Introduce an additional challenge where the rectangle must be axis-aligned.
FAQ
How do I solve the Minimum Area Rectangle II problem efficiently?
The key to an efficient solution is using array scanning combined with hash table lookups to check for potential rectangles and minimize the time complexity.
What is the expected time complexity for solving this problem?
The time complexity depends on the approach, but in the worst case, it could be O(n^2) due to the need to check pairs of points.
Why is hash table lookup important in this problem?
Hash table lookup speeds up the process of checking whether a point exists when validating possible rectangles, thus improving efficiency.
What happens if no rectangle can be formed from the points?
If no valid rectangle can be formed, the answer should be 0 as there is no valid rectangle configuration.
Can the sides of the rectangle be aligned with the X and Y axes?
No, the sides of the rectangle are not required to be parallel to the X and Y axes. The solution considers all possible orientations of the rectangle.
Solution
Solution 1: Hash Table + Enumeration
We use a hash table to store all the points, then enumerate three points $p_1 = (x_1, y_1)$, $p_2 = (x_2, y_2)$, $p_3 = (x_3, y_3)$, where $p_2$ and $p_3$ are the two endpoints of the diagonal of the rectangle. If the line formed by $p_1$ and $p_2$ and the line formed by $p_1$ and $p_3$ are perpendicular, and the fourth point $(x_4, y_4)=(x_2 - x_1 + x_3, y_2 - y_1 + y_3)$ exists in the hash table, then we have found a rectangle. At this point, we can calculate the area of the rectangle and update the answer.
class Solution:
def minAreaFreeRect(self, points: List[List[int]]) -> float:
s = {(x, y) for x, y in points}
n = len(points)
ans = inf
for i in range(n):
x1, y1 = points[i]
for j in range(n):
if j != i:
x2, y2 = points[j]
for k in range(j + 1, n):
if k != i:
x3, y3 = points[k]
x4 = x2 - x1 + x3
y4 = y2 - y1 + y3
if (x4, y4) in s:
v21 = (x2 - x1, y2 - y1)
v31 = (x3 - x1, y3 - y1)
if v21[0] * v31[0] + v21[1] * v31[1] == 0:
w = sqrt(v21[0] ** 2 + v21[1] ** 2)
h = sqrt(v31[0] ** 2 + v31[1] ** 2)
ans = min(ans, w * h)
return 0 if ans == inf else ansContinue Practicing
Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array scanning plus hash lookup
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Medium
Stay on this level to stabilize interview delivery.
arrow_forward