Last active
September 27, 2025 15:29
-
-
Save tatsuyax25/bf76110cbbf4c307666287d365db0aa5 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[][]} points | |
| * @return {number} | |
| */ | |
| var largestTriangleArea = function(points) { | |
| // Helper function to calculate the area of a triangle given three points | |
| function triangleArea(p1, p2, p3) { | |
| // Shoelace formula: | |
| // Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)| | |
| let [x1, y1] = p1; | |
| let [x2, y2] = p2; | |
| let [x3, y3] = p3; | |
| return Math.abs( | |
| x1 * (y2 - y3) + | |
| x2 * (y3 - y1) + | |
| x3 * (y1 - y2) | |
| ) / 2; | |
| } | |
| let maxArea = 0; | |
| // Try every combination of three different points | |
| 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++) { | |
| // Calculate the area of the triangle formed by points[i], points[j], points[k] | |
| let area = triangleArea(points[i], points[j], points[k]); | |
| // Update maxArea if this triangle is larger | |
| maxArea = Math.max(maxArea, area); | |
| } | |
| } | |
| } | |
| return maxArea; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment