Skip to content

Commit 399fed8

Browse files
committedMar 16, 2025
Add solution #812
1 parent eb253df commit 399fed8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@
620620
809|[Expressive Words](./0809-expressive-words.js)|Medium|
621621
810|[Chalkboard XOR Game](./0810-chalkboard-xor-game.js)|Hard|
622622
811|[Subdomain Visit Count](./0811-subdomain-visit-count.js)|Medium|
623+
812|[Largest Triangle Area](./0812-largest-triangle-area.js)|Easy|
623624
819|[Most Common Word](./0819-most-common-word.js)|Easy|
624625
821|[Shortest Distance to a Character](./0821-shortest-distance-to-a-character.js)|Easy|
625626
824|[Goat Latin](./0824-goat-latin.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 812. Largest Triangle Area
3+
* https://leetcode.com/problems/largest-triangle-area/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of points on the X-Y plane points where points[i] = [xi, yi], return the area of
7+
* the largest triangle that can be formed by any three different points. Answers within 10-5 of
8+
* the actual answer will be accepted.
9+
*/
10+
11+
/**
12+
* @param {number[][]} points
13+
* @return {number}
14+
*/
15+
var largestTriangleArea = function(points) {
16+
let maxArea = 0;
17+
18+
for (let i = 0; i < points.length; i++) {
19+
for (let j = i + 1; j < points.length; j++) {
20+
for (let k = j + 1; k < points.length; k++) {
21+
const area = calculateTriangleArea(points[i], points[j], points[k]);
22+
maxArea = Math.max(maxArea, area);
23+
}
24+
}
25+
}
26+
27+
return maxArea;
28+
};
29+
30+
function calculateTriangleArea(p1, p2, p3) {
31+
const [[x1, y1], [x2, y2], [x3, y3]] = [p1, p2, p3];
32+
return Math.abs(
33+
(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2
34+
);
35+
}

0 commit comments

Comments
 (0)
Please sign in to comment.