forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum Area Rectangle.js
36 lines (30 loc) · 1.26 KB
/
Minimum Area Rectangle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var minAreaRect = function(points) {
const mapOfPoints = new Map();
let minArea = Infinity;
for(const [x,y] of points) {
let keyString = `${x}:${y}`
mapOfPoints.set(keyString, [x, y]);
}
for(const [xLeftBottom, yLeftBottom] of points) {
for(const [xRightTop, yRightTop] of points) {
if(!foundDiagonal(xLeftBottom, yLeftBottom, xRightTop, yRightTop)) continue;
let leftTopCorner = `${xLeftBottom}:${yRightTop}`;
let rightBottomCorner = `${xRightTop}:${yLeftBottom}`;
if(mapOfPoints.has(leftTopCorner) && mapOfPoints.has(rightBottomCorner)) {
const x2 = mapOfPoints.get(rightBottomCorner)[0];
const x1 = xLeftBottom;
const y1 = yLeftBottom;
const y2 = mapOfPoints.get(leftTopCorner)[1]
const area = calculateArea(x1, x2, y1, y2);
minArea = Math.min(minArea,area);
}
}
}
return minArea === Infinity ? 0 : minArea;
};
function calculateArea(x1, x2, y1, y2) {
return ((x2-x1) * (y2-y1))
}
function foundDiagonal(xLeftBottom, yLeftBottom, xRightTop, yRightTop) {
return (xRightTop > xLeftBottom && yRightTop > yLeftBottom);
}