-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0812-largest-triangle-area.js
More file actions
35 lines (31 loc) · 968 Bytes
/
0812-largest-triangle-area.js
File metadata and controls
35 lines (31 loc) · 968 Bytes
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
/**
* 812. Largest Triangle Area
* https://leetcode.com/problems/largest-triangle-area/
* Difficulty: Easy
*
* Given an array of points on the X-Y plane points where points[i] = [xi, yi], return the area of
* the largest triangle that can be formed by any three different points. Answers within 10-5 of
* the actual answer will be accepted.
*/
/**
* @param {number[][]} points
* @return {number}
*/
var largestTriangleArea = function(points) {
let maxArea = 0;
for (let i = 0; i < points.length; i++) {
for (let j = i + 1; j < points.length; j++) {
for (let k = j + 1; k < points.length; k++) {
const area = calculateTriangleArea(points[i], points[j], points[k]);
maxArea = Math.max(maxArea, area);
}
}
}
return maxArea;
};
function calculateTriangleArea(p1, p2, p3) {
const [[x1, y1], [x2, y2], [x3, y3]] = [p1, p2, p3];
return Math.abs(
(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2
);
}