Problem Description
Given an array of unique points in the X-Y plane, return the minimum area of a rectangle that can be formed from these points, with sides parallel to the X and Y axes. If no valid rectangle exists, return 0.
Key Insights
- Use a hash set (or equivalent) to store points for O(1) lookup.
- Iterate over all pairs of points that could form opposite corners of a rectangle (ensuring they have different x and y values).
- For each such pair, check if the other two required points exist.
- Keep track of the smallest area found; if no rectangle is detected, return 0.
Space and Time Complexity
Time Complexity: O(n^2) where n is the number of points (each pair is inspected). Space Complexity: O(n) for storing the points in a hash set.
Solution
The solution involves iterating through every pair of points. For each pair with distinct x and y coordinates (potential diagonal corners of a rectangle), check if the other two corners (with swapped y and x values) exist in the set. If they do, calculate the area and update the minimum area if it is smaller than the current minimum. If no valid rectangle is found after checking all pairs, return 0.