-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0812-largest-triangle-area.js
More file actions
59 lines (50 loc) · 1.59 KB
/
0812-largest-triangle-area.js
File metadata and controls
59 lines (50 loc) · 1.59 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
/**
* Largest Triangle Area
* Time Complexity: O(N^3)
* Space Complexity: O(1)
*/
var largestTriangleArea = function (points) {
let greatestAreaValue = 0;
const totalPointsCount = points.length;
for (
let firstPointIteration = 0;
firstPointIteration < totalPointsCount;
firstPointIteration++
) {
for (
let secondPointIteration = firstPointIteration + 1;
secondPointIteration < totalPointsCount;
secondPointIteration++
) {
for (
let thirdPointIteration = secondPointIteration + 1;
thirdPointIteration < totalPointsCount;
thirdPointIteration++
) {
const pointCoordinateA = points[firstPointIteration];
const pointCoordinateB = points[secondPointIteration];
const pointCoordinateC = points[thirdPointIteration];
const currentTriangleArea = calculateGeometryArea(
pointCoordinateA,
pointCoordinateB,
pointCoordinateC,
);
greatestAreaValue = Math.max(greatestAreaValue, currentTriangleArea);
}
}
}
return greatestAreaValue;
};
function calculateGeometryArea(firstVertex, secondVertex, thirdVertex) {
const firstCoordX = firstVertex[0];
const firstCoordY = firstVertex[1];
const secondCoordX = secondVertex[0];
const secondCoordY = secondVertex[1];
const thirdCoordX = thirdVertex[0];
const thirdCoordY = thirdVertex[1];
const determinantResult =
firstCoordX * (secondCoordY - thirdCoordY) +
secondCoordX * (thirdCoordY - firstCoordY) +
thirdCoordX * (firstCoordY - secondCoordY);
return Math.abs(determinantResult / 2);
}