1
var minAreaRect = function (points) {2
const mapOfPoints = new Map();3
let minArea = Infinity;4
for (const [x, y] of points) {5
let keyString = `${x}:${y}`;6
mapOfPoints.set(keyString, [x, y]);8
for (const [xLeftBottom, yLeftBottom] of points) {9
for (const [xRightTop, yRightTop] of points) {10
if (!foundDiagonal(xLeftBottom, yLeftBottom, xRightTop, yRightTop))12
let leftTopCorner = `${xLeftBottom}:${yRightTop}`;13
let rightBottomCorner = `${xRightTop}:${yLeftBottom}`;16
mapOfPoints.has(leftTopCorner) &&17
mapOfPoints.has(rightBottomCorner)19
const x2 = mapOfPoints.get(rightBottomCorner)[0];20
const x1 = xLeftBottom;21
const y1 = yLeftBottom;22
const y2 = mapOfPoints.get(leftTopCorner)[1];23
const area = calculateArea(x1, x2, y1, y2);25
minArea = Math.min(minArea, area);29
return minArea === Infinity ? 0 : minArea;32
function calculateArea(x1, x2, y1, y2) {33
return (x2 - x1) * (y2 - y1);36
function foundDiagonal(xLeftBottom, yLeftBottom, xRightTop, yRightTop) {37
return xRightTop > xLeftBottom && yRightTop > yLeftBottom;