Leetcode 939: Minimum Area Rectangle

grid47
grid47
Exploring patterns and algorithms
Aug 5, 2024 6 min read

You are given a set of points in a 2D plane, represented by an array where each point is a pair of integers [xi, yi]. Your task is to find the minimum area of a rectangle that can be formed using these points, with sides parallel to the X and Y axes. If no such rectangle can be formed, return 0.
Problem
Approach
Steps
Complexity
Input: The input consists of a 2D array where each element represents a point in the X-Y plane. Each point is a pair of integers [xi, yi].
Example: Input: points = [[1,1],[2,2],[3,3],[1,3],[3,1]]
Constraints:
• 1 <= points.length <= 500
• points[i].length == 2
• 0 <= xi, yi <= 40000
• All the given points are unique.
Output: Return the minimum area of the rectangle formed by these points, or 0 if no such rectangle exists.
Example: Output: 4
Constraints:
• If no rectangle can be formed, return 0.
Goal: The goal is to identify all possible rectangles formed by four distinct points. The sides of the rectangle should be parallel to the X and Y axes, so we need to check for pairs of points with the same X or Y coordinates.
Steps:
• 1. Store the points in a hash map grouped by their X-coordinates.
• 2. For each pair of points, check if the remaining two points can form a rectangle with sides parallel to the axes.
• 3. Calculate the area of the rectangle formed by the points.
• 4. Keep track of the minimum area found.
Goal: The constraints ensure that the input contains a manageable number of points, and that each point is distinct.
Steps:
• The number of points will not exceed 500.
• Coordinates are within the range [0, 40000].
Assumptions:
• All points are distinct and can potentially form a rectangle.
• The rectangle sides are parallel to the X and Y axes.
Input: Input: points = [[1,1],[2,2],[3,3],[1,3],[3,1]]
Explanation: In this case, the points (1,1), (1,3), (3,1), and (3,3) form a rectangle. The minimum area is calculated as (3 - 1) * (3 - 1) = 4.

Input: Input: points = [[1,1],[1,2],[2,2],[2,1]]
Explanation: The points form a perfect rectangle with sides parallel to the axes. The area is (2 - 1) * (2 - 1) = 1.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus