Minimum Area Rectangle LeetCode solution in JavaScript

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]);
7
}
8
for (const [xLeftBottom, yLeftBottom] of points) {
9
for (const [xRightTop, yRightTop] of points) {
10
if (!foundDiagonal(xLeftBottom, yLeftBottom, xRightTop, yRightTop))
11
continue;
12
let leftTopCorner = `${xLeftBottom}:${yRightTop}`;
13
let rightBottomCorner = `${xRightTop}:${yLeftBottom}`;
14

15
if (
16
mapOfPoints.has(leftTopCorner) &&
17
mapOfPoints.has(rightBottomCorner)
18
) {
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);
24

25
minArea = Math.min(minArea, area);
26
}
27
}
28
}
29
return minArea === Infinity ? 0 : minArea;
30
};
31

32
function calculateArea(x1, x2, y1, y2) {
33
return (x2 - x1) * (y2 - y1);
34
}
35

36
function foundDiagonal(xLeftBottom, yLeftBottom, xRightTop, yRightTop) {
37
return xRightTop > xLeftBottom && yRightTop > yLeftBottom;
38
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0