Minimum Area Rectangle - Solution & Explanation
Problem Statement
You are given an array of points in the X-Y plane points where points[i] = [xi, yi].
Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is not any such rectangle, return 0.
Example 1:
Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]] Output: 4
Example 2:
Input: points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]] Output: 2
Constraints:
1 <= points.length <= 500points[i].length == 20 <= xi, yi <= 4 * 104- All the given points are unique.
Approach Overview
Problem Overview: You receive a list of 2D points on a grid. Four of those points can form an axis-aligned rectangle if they share two distinct x-coordinates and two distinct y-coordinates. The goal is to compute the smallest possible rectangle area formed by any four points. If no rectangle exists, return 0.
Approach 1: Using Dynamic Programming (O(n^2) time, O(n^2) space)
Group points by their x coordinate so each column contains the y values that appear at that x-position. Sort the y values within each column. For every pair of (y1, y2) in the same column, treat it as a potential vertical edge of a rectangle. Maintain a DP structure dp[y1][y2] that stores the last x coordinate where this pair appeared. When the same pair appears in a new column, you have two vertical edges. The rectangle area becomes (x - dp[y1][y2]) * (y2 - y1). Update the minimum area and refresh the stored column.
This works because a rectangle is uniquely determined by two vertical edges with identical y pairs. The DP table remembers where each vertical edge pair previously occurred, allowing constant-time area calculation when the pair reappears. The approach relies heavily on fast lookups using a structure similar to a hash table while iterating through sorted columns of points.
Approach 2: Optimizing Space using Two Variables (O(n^2) time, O(n) space)
The full DP table can grow large if many unique y values exist. Instead of storing a dense 2D matrix for dp[y1][y2], track pairs using a compact key such as (y1, y2) in a hash map. For each column, iterate through all combinations of two y values. The map stores the last column where that pair appeared. When encountered again, compute the area using the previous column and update the minimum rectangle.
This optimization removes the need for a large DP matrix while preserving the same logic. Each pair is processed using constant-time hash lookups. The algorithm still scans all vertical pairs in each column, so the time complexity remains O(n^2), but memory drops significantly because only observed pairs are stored. The approach fits well for sparse coordinate distributions often seen in geometry problems.
Recommended for interviews: Interviewers typically expect the hash-based pair tracking approach (Approach 2). A brute-force rectangle check over all point quadruples shows basic understanding but is too slow. Demonstrating the vertical edge insight and storing (y1, y2) pairs in a array/hash structure shows strong problem-solving and familiarity with geometric pattern recognition.
Approach 1: Approach 1: Using Dynamic Programming
This approach involves using dynamic programming to efficiently solve the problem by breaking it down into overlapping subproblems. The solution stores the results of subproblems to avoid unnecessary recalculations, making it more efficient.
This C program uses dynamic programming to build up an array where each element at index i stores the maximum sum thus far, considering whether to include the current element based on prior calculations. It efficiently computes the solution in linear time.
Complexity
Time Complexity: O(n)
Space Complexity: O(n)
Approach 2: Approach 2: Optimizing Space using Two Variables
This approach is a refinement of the dynamic programming method, designed to use less space. Instead of maintaining an entire array to keep track of intermediate results, we only keep two variables to store the most recent results.
This C implementation uses two variables to store the maximum sums of subarrays, thereby significantly reducing space usage compared to the full dynamic programming approach.
Complexity
Time Complexity: O(n)
Space Complexity: O(1)
Approach 3: Default Approach
Try this approach in the editor →Complexity Comparison
| Approach | Complexity |
|---|---|
| Approach 1: Using Dynamic Programming | Time Complexity: O(n) |
| Approach 2: Optimizing Space using Two Variables | Time Complexity: O(n) |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with 2D Pair Table | O(n^2) | O(n^2) | Useful for understanding the rectangle detection idea with explicit state tracking for every (y1, y2) pair. |
| Hash Map Pair Tracking (Space Optimized) | O(n^2) | O(n) | Preferred solution in interviews and production when the number of unique y-pairs is sparse. |
Video Solution
MINIMUM AREA RECTANGLE | GOOGLE INTERVIEW QUESTION | PYTHON SOLUTION • Cracking FAANG • 18,719 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Minimum Area Rectangle easy or hard?
How to solve Minimum Area Rectangle in O(n^2)?
What is the best approach for Minimum Area Rectangle?
What data structure is used in Minimum Area Rectangle?
What is the time complexity of Minimum Area Rectangle?
Minimum Area Rectangle Python or Java solution approach?
Is Minimum Area Rectangle asked at Google, Amazon, or Meta?
Ready to solve this problem?
Practice Minimum Area Rectangle with our built-in code editor and test cases.
Practice on FleetCode