Minimum Area Rectangle - Problem

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
Minimum Area Rectangle: Diagonal Corner MethodStep 1: Hash All PointsStore in hash set:โ€ข (1,1) โœ“โ€ข (1,3) โœ“โ€ข (3,1) โœ“โ€ข (3,3) โœ“โ€ข (4,2) โœ“O(1) lookup time!Valid Rectangle Found!Area = 2 ร— 2 = 4Step 2: Test DiagonalsFor diagonal (1,1)โ†”(3,3):Need corners at:โ€ข (1,3) โœ“ existsโ€ข (3,1) โœ“ existsValid rectangle!Area = 2 ร— 2 = 4Continue checking...
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!
Asked in
Google 47 Amazon 34 Microsoft 28 Meta 22
67.2K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen