Advertisement

Minimum Area Rectangle - LeetCode 939 Solution

Minimum Area Rectangle - Complete Solution Guide

Minimum Area Rectangle is LeetCode problem 939, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array of points in the X-Y plane points where points[i] = [x i , y i ] . 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 <= x i , y i <= 4 * 10 4 All the given points are

Detailed Explanation

The problem asks us to find the minimum area of a rectangle formed by a given set of points in the XY plane, with the constraint that the rectangle's sides must be parallel to the X and Y axes. If no such rectangle can be formed from the given points, we should return 0. The input is an array of points, where each point is represented by a pair of coordinates (x, y).

Solution Approach

The solution iterates through all possible pairs of points in the input array. For each pair, it checks if these points can form two corners of a rectangle with sides parallel to the axes (i.e., they do not have the same x or y coordinates). If they can, it checks if the other two corners of the rectangle exist within the given set of points. If all four corners exist, the area of the rectangle is calculated, and the minimum area is updated accordingly. A set is used to efficiently check if a point exists.

Step-by-Step Algorithm

  1. Step 1: Create a set (or hash table) containing all the given points for efficient lookup.
  2. Step 2: Iterate through all possible pairs of points (p1, p2) from the input array.
  3. Step 3: For each pair of points (p1(x1, y1), p2(x2, y2)), check if x1 != x2 and y1 != y2. This condition ensures that the two points can be diagonally opposite corners of a rectangle.
  4. Step 4: If the condition in Step 3 is met, check if the other two potential corners of the rectangle, (x1, y2) and (x2, y1), are present in the point set.
  5. Step 5: If both corners (x1, y2) and (x2, y1) exist in the set, calculate the area of the rectangle as abs(x1 - x2) * abs(y1 - y2).
  6. Step 6: Update the minimum area found so far if the calculated area is smaller.
  7. Step 7: After iterating through all pairs of points, return the minimum area. If no rectangle was found, return 0.

Key Insights

  • Insight 1: To form a rectangle with sides parallel to the axes, we need to find four points such that two pairs of points have the same x-coordinate and the other two pairs have the same y-coordinate.
  • Insight 2: Using a set or hash table to store the points allows for efficient checking of whether a potential rectangle's corners exist within the input point set.
  • Insight 3: We can iterate through all possible pairs of points and check if they can be two corners of a rectangle. If they are, we check if the other two corners exist in the set of points.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Math, Geometry, Sorting.

Companies

Asked at: ByteDance, Flipkart, Snap, Verily.