Skip to main content

Minimum Area Rectangle - Solution & Explanation

MediumArrayHash TableMathGeometry13 min readAsked at: Microsoft, Meta, Google +5
Practice this problem

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 <= 500
  • points[i].length == 2
  • 0 <= 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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n)

Try this approach in the editor →

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Using Dynamic Programming

Time Complexity: O(n)
Space Complexity: O(n)

Approach 2: Optimizing Space using Two Variables

Time Complexity: O(n)
Space Complexity: O(1)

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Dynamic Programming with 2D Pair TableO(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?
Minimum Area Rectangle is typically classified as a Medium difficulty problem. The challenge lies in recognizing that rectangles can be detected by matching vertical edge pairs across columns rather than checking every combination of four points.
How to solve Minimum Area Rectangle in O(n^2)?
Group points by their x-coordinate, then sort the y-values within each column. For every pair (y1, y2), store the last x-coordinate where this pair appeared in a hash map. When the pair appears again, compute the rectangle area using the difference in x and y coordinates and update the minimum area.
What is the best approach for Minimum Area Rectangle?
The best approach groups points by x-coordinate and tracks vertical edge pairs (y1, y2) using a hash map. When the same pair appears in another column, it forms a rectangle and the area can be computed instantly. This method runs in O(n^2) time with roughly O(n) space and avoids checking every quadruple of points.
What data structure is used in Minimum Area Rectangle?
The core data structure is a hash map that stores pairs of y-coordinates as keys and the last x-coordinate where the pair appeared as the value. Arrays or lists are used to group points by column, and sorting helps process y-pairs in order.
What is the time complexity of Minimum Area Rectangle?
The optimal solution runs in O(n^2) time. For each column, the algorithm checks all pairs of y-values and uses a hash lookup to see if that pair appeared in a previous column. Space complexity is typically O(n) when using a hash map to store previously seen pairs.
Minimum Area Rectangle Python or Java solution approach?
Both Python and Java implementations follow the same strategy: group points by x-coordinate, generate y-pairs for each column, and store them in a hash map. When a pair reappears in a new column, calculate the rectangle area and update the minimum. The complexity remains O(n^2).
Is Minimum Area Rectangle asked at Google, Amazon, or Meta?
Minimum Area Rectangle is a common geometry and hash map interview problem and has appeared in interviews at companies such as Google, Amazon, and Meta. It tests the ability to recognize geometric patterns and optimize brute-force rectangle detection using hash-based lookups.

Ready to solve this problem?

Practice Minimum Area Rectangle with our built-in code editor and test cases.

Practice on FleetCode