🧠
Brute Force (Check All Quadruples)
💡 This approach exists to build intuition by trying every possible combination of four points to check if they form a rectangle. It is too slow for large inputs but helps understand the problem deeply.
Intuition
Try every set of four points and check if they form a rectangle with sides parallel to axes. Calculate the area if they do, and track the minimum.
Algorithm
- Enumerate all quadruples of points.
- For each quadruple, check if they form a rectangle with sides parallel to axes.
- If yes, compute the area and update the minimum area found.
- Return the minimum area or 0 if none found.
💡 The challenge is to correctly identify rectangles and handle all quadruples, which is straightforward but computationally expensive.
from itertools import combinations
def minAreaRect(points):
point_set = set(map(tuple, points))
min_area = float('inf')
for p1, p2, p3, p4 in combinations(points, 4):
xs = {p1[0], p2[0], p3[0], p4[0]}
ys = {p1[1], p2[1], p3[1], p4[1]}
if len(xs) == 2 and len(ys) == 2:
# Check all four corners exist
corners = [(x, y) for x in xs for y in ys]
if all(c in point_set for c in corners):
area = abs(max(xs) - min(xs)) * abs(max(ys) - min(ys))
if area < min_area:
min_area = area
return 0 if min_area == float('inf') else min_area
# Driver code
points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
print(minAreaRect(points))
Line Notes
from itertools import combinationsImport combinations to generate all quadruples of points
point_set = set(map(tuple, points))Convert points to a set for O(1) membership checks
for p1, p2, p3, p4 in combinations(points, 4)Enumerate every group of 4 points to check rectangles
xs = {p1[0], p2[0], p3[0], p4[0]}Collect distinct x-coordinates of the quadruple
ys = {p1[1], p2[1], p3[1], p4[1]}Collect distinct y-coordinates of the quadruple
if len(xs) == 2 and len(ys) == 2A rectangle aligned with axes must have exactly 2 distinct x and y coordinates
corners = [(x, y) for x in xs for y in ys]Generate all four corners of the rectangle
if all(c in point_set for c in corners)Verify all four corners exist in the input points
area = abs(max(xs) - min(xs)) * abs(max(ys) - min(ys))Calculate area of the rectangle
return 0 if min_area == float('inf') else min_areaReturn 0 if no rectangle found
import java.util.*;
public class MinAreaRectBrute {
private static Set<Integer> getDistinct(Set<Integer> set, int... vals) {
for (int v : vals) set.add(v);
return set;
}
public static int minAreaRect(int[][] points) {
Set<String> pointSet = new HashSet<>();
for (int[] p : points) {
pointSet.add(p[0] + "," + p[1]);
}
int minArea = Integer.MAX_VALUE;
int n = points.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
for (int l = k + 1; l < n; l++) {
int[] p1 = points[i], p2 = points[j], p3 = points[k], p4 = points[l];
Set<Integer> xs = new HashSet<>();
xs.add(p1[0]); xs.add(p2[0]); xs.add(p3[0]); xs.add(p4[0]);
Set<Integer> ys = new HashSet<>();
ys.add(p1[1]); ys.add(p2[1]); ys.add(p3[1]); ys.add(p4[1]);
if (xs.size() == 2 && ys.size() == 2) {
List<String> corners = new ArrayList<>();
for (int x : xs) {
for (int y : ys) {
corners.add(x + "," + y);
}
}
boolean allExist = true;
for (String c : corners) {
if (!pointSet.contains(c)) {
allExist = false;
break;
}
}
if (allExist) {
int area = (Collections.max(xs) - Collections.min(xs)) * (Collections.max(ys) - Collections.min(ys));
if (area < minArea) minArea = area;
}
}
}
}
}
}
return minArea == Integer.MAX_VALUE ? 0 : minArea;
}
public static void main(String[] args) {
int[][] points = {{1,1},{1,3},{3,1},{3,3},{2,2}};
System.out.println(minAreaRect(points));
}
}
Line Notes
Set<String> pointSet = new HashSet<>();Store points as strings for quick lookup
for (int i = 0; i < n; i++)Outer loop to pick first point
for (int j = i + 1; j < n; j++)Second point of quadruple
for (int k = j + 1; k < n; k++)Third point of quadruple
for (int l = k + 1; l < n; l++)Fourth point of quadruple
Set<Integer> xs = new HashSet<>(); xs.add(...)Collect distinct x-coordinates of quadruple
Set<Integer> ys = new HashSet<>(); ys.add(...)Collect distinct y-coordinates of quadruple
if (xs.size() == 2 && ys.size() == 2)Check if quadruple can form rectangle corners
for (int x : xs) { for (int y : ys) {Generate all four corners of rectangle
if (!pointSet.contains(c))Verify all corners exist in input
int area = (Collections.max(xs) - Collections.min(xs)) * (Collections.max(ys) - Collections.min(ys));Calculate rectangle area
return minArea == Integer.MAX_VALUE ? 0 : minArea;Return 0 if no rectangle found
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
using namespace std;
string toStr(int x, int y) {
return to_string(x) + "," + to_string(y);
}
int minAreaRect(vector<vector<int>>& points) {
unordered_set<string> pointSet;
for (auto& p : points) {
pointSet.insert(toStr(p[0], p[1]));
}
int n = points.size();
int minArea = INT_MAX;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
for (int l = k + 1; l < n; l++) {
vector<int> xs = {points[i][0], points[j][0], points[k][0], points[l][0]};
vector<int> ys = {points[i][1], points[j][1], points[k][1], points[l][1]};
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
if (xs.size() == 2 && ys.size() == 2) {
bool allExist = true;
for (int x : xs) {
for (int y : ys) {
if (pointSet.find(toStr(x, y)) == pointSet.end()) {
allExist = false;
break;
}
}
if (!allExist) break;
}
if (allExist) {
int area = (xs[1] - xs[0]) * (ys[1] - ys[0]);
if (area < minArea) minArea = area;
}
}
}
}
}
}
return minArea == INT_MAX ? 0 : minArea;
}
int main() {
vector<vector<int>> points = {{1,1},{1,3},{3,1},{3,3},{2,2}};
cout << minAreaRect(points) << endl;
return 0;
}
Line Notes
unordered_set<string> pointSet;Use hash set for O(1) point existence checks
for (int i = 0; i < n; i++)Outer loop picks first point
for (int j = i + 1; j < n; j++)Second point of quadruple
for (int k = j + 1; k < n; k++)Third point of quadruple
for (int l = k + 1; l < n; l++)Fourth point of quadruple
sort(xs.begin(), xs.end()); xs.erase(...)Remove duplicates to get distinct x-coordinates
sort(ys.begin(), ys.end()); ys.erase(...)Remove duplicates to get distinct y-coordinates
if (xs.size() == 2 && ys.size() == 2)Check if quadruple can form rectangle corners
for (int x : xs) { for (int y : ys) {Check all four corners exist
int area = (xs[1] - xs[0]) * (ys[1] - ys[0]);Calculate rectangle area
return minArea == INT_MAX ? 0 : minArea;Return 0 if no rectangle found
function minAreaRect(points) {
const pointSet = new Set(points.map(p => p[0] + ',' + p[1]));
let minArea = Infinity;
const n = points.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = j + 1; k < n; k++) {
for (let l = k + 1; l < n; l++) {
const xs = new Set([points[i][0], points[j][0], points[k][0], points[l][0]]);
const ys = new Set([points[i][1], points[j][1], points[k][1], points[l][1]]);
if (xs.size === 2 && ys.size === 2) {
const xsArr = Array.from(xs);
const ysArr = Array.from(ys);
let allExist = true;
for (const x of xsArr) {
for (const y of ysArr) {
if (!pointSet.has(x + ',' + y)) {
allExist = false;
break;
}
}
if (!allExist) break;
}
if (allExist) {
const area = Math.abs(xsArr[0] - xsArr[1]) * Math.abs(ysArr[0] - ysArr[1]);
if (area < minArea) minArea = area;
}
}
}
}
}
}
return minArea === Infinity ? 0 : minArea;
}
// Test
console.log(minAreaRect([[1,1],[1,3],[3,1],[3,3],[2,2]]));
Line Notes
const pointSet = new Set(points.map(p => p[0] + ',' + p[1]))Create a set for O(1) point lookup
for (let i = 0; i < n; i++)Outer loop picks first point
for (let j = i + 1; j < n; j++)Second point of quadruple
for (let k = j + 1; k < n; k++)Third point of quadruple
for (let l = k + 1; l < n; l++)Fourth point of quadruple
const xs = new Set([...])Collect distinct x-coordinates of quadruple
const ys = new Set([...])Collect distinct y-coordinates of quadruple
if (xs.size === 2 && ys.size === 2)Check if quadruple can form rectangle corners
for (const x of xsArr) { for (const y of ysArr) {Verify all four corners exist
const area = Math.abs(xsArr[0] - xsArr[1]) * Math.abs(ysArr[0] - ysArr[1])Calculate rectangle area
return minArea === Infinity ? 0 : minAreaReturn 0 if no rectangle found
We check all quadruples of points, which is O(n^4). Storing points in a set costs O(n) space.
💡 For n=20, this means about 160,000 operations, which is borderline but grows too fast for larger n.
Interview Verdict: TLE
This approach is too slow for large inputs but is useful to understand the problem and correctness before optimizing.