812. Largest Triangle Area #
Problem #
You have a list of points in the plane. Return the area of the largest triangle that can be formed by any 3 of the points.
Example:
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
Output: 2
Explanation:
The five points are show in the figure below. The red triangle is the largest.

Notes:
3 <= points.length <= 50.- No points will be duplicated.
-50 <= points[i][j] <= 50.- Answers within
10^-6of the true value will be accepted as correct.
Problem Summary #
Given a set containing multiple points, choose three points from it to form a triangle, and return the area of the largest triangle that can be formed.
Solution Approach #
- Given a set of point coordinates, find the set of points that can form the triangle with the largest area, and output this maximum area.
- Math problem. According to the mathematical definition, calculate the areas of the triangles formed by the points that can form triangles, and finally output the maximum area.
Code #
package leetcode
func largestTriangleArea(points [][]int) float64 {
maxArea, n := 0.0, len(points)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for k := j + 1; k < n; k++ {
maxArea = max(maxArea, area(points[i], points[j], points[k]))
}
}
}
return maxArea
}
func area(p1, p2, p3 []int) float64 {
return abs(p1[0]*p2[1]+p2[0]*p3[1]+p3[0]*p1[1]-p1[0]*p3[1]-p2[0]*p1[1]-p3[0]*p2[1]) / 2
}
func abs(num int) float64 {
if num < 0 {
num = -num
}
return float64(num)
}
func max(a, b float64) float64 {
if a > b {
return a
}
return b
}