Leetcode 963: Minimum Area Rectangle II

grid47
grid47
Exploring patterns and algorithms
Aug 2, 2024 7 min read

You are given a set of points in a 2D plane, represented as an array where each point is in the format [xi, yi]. Your task is to find the minimum possible area of any rectangle formed by these points, where the sides of the rectangle do not need to be parallel to the X or Y axes. If no rectangle can be formed, return 0. Answers within 10^-5 of the actual area are considered correct.
Problem
Approach
Steps
Complexity
Input: The input is an array of unique points in a 2D plane. Each point is represented as a pair of integers [xi, yi], where xi and yi denote the coordinates of the point.
Example: Input: points = [[0,2],[1,1],[2,0],[1,3],[3,1]]
Constraints:
• 1 <= points.length <= 50
• points[i].length == 2
• 0 <= xi, yi <= 40,000
• All points are unique.
Output: The output is a floating-point number representing the minimum possible area of a rectangle formed by the given points. If no rectangle can be formed, return 0.
Example: Output: 1.00000
Constraints:
• The result must be accurate within 10^-5 of the actual area.
Goal: Find the minimum area of a rectangle that can be formed using the given points. If no rectangle exists, return 0.
Steps:
• 1. Use combinations of two points to define potential rectangle sides.
• 2. Use a hashing technique to group points by their center and length for efficient pair comparisons.
• 3. Calculate the area for each possible rectangle, keeping track of the minimum value.
• 4. If no rectangle is formed, return 0.
Goal: The input array will contain between 1 and 50 points. All points are unique and lie within the range [0, 40,000] on both axes.
Steps:
• 1 <= points.length <= 50
• 0 <= xi, yi <= 40,000
Assumptions:
• The input points are unique and will not have duplicates.
• It is possible for no rectangle to be formed, in which case the output should be 0.
Input: Input: points = [[0,2],[2,0],[1,1],[3,3]]
Explanation: The minimum area rectangle is formed by points [0,2],[2,0],[1,1],[3,3] with an area of 2.00000.

Input: Input: points = [[1,0],[2,2],[3,1],[0,3]]
Explanation: No valid rectangle can be formed from these points, so the output is 0.

Input: Input: points = [[0,0],[0,2],[2,0],[2,2]]
Explanation: The minimum area rectangle is formed by points [0,0],[0,2],[2,0],[2,2] with an area of 4.00000.

Link to LeetCode Lab


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