Math Fundamentals18 sections · 814 units
Open in Course

Minimum Area Rectangle - Implementation

The pseudocode

Here is the full solution:


function minAreaRect(points)
    pointSet := set of all (x, y) tuples
    minArea := infinity
    n := length of points
    for i from 0 to n - 1
        for j from i + 1 to n - 1
            (x1, y1) := points[i]
            (x2, y2) := points[j]
            if x1 != x2 and y1 != y2 then
                if (x1, y2) in pointSet and (x2, y1) in pointSet then
                    area := abs(x2 - x1) * abs(y2 - y1)
                    minArea := min(minArea, area)
    if minArea = infinity then
        return 0
    return minArea

Time: O(n2)O(n^2) to check all pairs. Space: O(n)O(n) for the point set.