-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0963-minimum-area-rectangle-ii.js
More file actions
93 lines (78 loc) · 2.85 KB
/
0963-minimum-area-rectangle-ii.js
File metadata and controls
93 lines (78 loc) · 2.85 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Minimum Area Rectangle II
* Time Complexity: O(N^3)
* Space Complexity: O(N)
*/
var minAreaFreeRect = function (points) {
const allPointsArray = points;
const totalPointsCount = allPointsArray.length;
const existingPointsSet = new Set();
for (
let currentPointSetIndex = 0;
currentPointSetIndex < totalPointsCount;
currentPointSetIndex++
) {
const currentPointSetElement = allPointsArray[currentPointSetIndex];
existingPointsSet.add(
`${currentPointSetElement[0]},${currentPointSetElement[1]}`,
);
}
let minRectangleArea = Infinity;
let firstPointIterator = 0;
while (firstPointIterator < totalPointsCount) {
const firstPoint = allPointsArray[firstPointIterator];
const firstPointXCoordinate = firstPoint[0];
const firstPointYCoordinate = firstPoint[1];
let secondPointIterator = firstPointIterator + 1;
while (secondPointIterator < totalPointsCount) {
const secondPoint = allPointsArray[secondPointIterator];
const secondPointXCoordinate = secondPoint[0];
const secondPointYCoordinate = secondPoint[1];
const vecOneDx = secondPointXCoordinate - firstPointXCoordinate;
const vecOneDy = secondPointYCoordinate - firstPointYCoordinate;
for (
let thirdPointIterator = 0;
thirdPointIterator < totalPointsCount;
thirdPointIterator++
) {
if (
thirdPointIterator === firstPointIterator ||
thirdPointIterator === secondPointIterator
) {
continue;
}
const thirdPoint = allPointsArray[thirdPointIterator];
const thirdPointXCoordinate = thirdPoint[0];
const thirdPointYCoordinate = thirdPoint[1];
const vecTwoDx = thirdPointXCoordinate - firstPointXCoordinate;
const vecTwoDy = thirdPointYCoordinate - firstPointYCoordinate;
if (vecOneDx * vecTwoDx + vecOneDy * vecTwoDy !== 0) {
continue;
}
const anticipatedFourthPointX = secondPointXCoordinate + vecTwoDx;
const anticipatedFourthPointY = secondPointYCoordinate + vecTwoDy;
if (
existingPointsSet.has(
`${anticipatedFourthPointX},${anticipatedFourthPointY}`,
)
) {
const lengthOneSquared = vecOneDx * vecOneDx + vecOneDy * vecOneDy;
const lengthTwoSquared = vecTwoDx * vecTwoDx + vecTwoDy * vecTwoDy;
const computedArea =
Math.sqrt(lengthOneSquared) * Math.sqrt(lengthTwoSquared);
if (computedArea < 1e-9) {
continue;
}
if (minRectangleArea === Infinity) {
minRectangleArea = computedArea;
} else {
minRectangleArea = Math.min(minRectangleArea, computedArea);
}
}
}
secondPointIterator++;
}
firstPointIterator++;
}
return minRectangleArea === Infinity ? 0 : minRectangleArea;
};