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^-6
of the true value will be accepted as correct.
实现思路:这道题给了我们一系列的二维平面上的点,让我们找出任意三个点能组成的最大三角形的面积。那么我们只能遍历所有的三角形面积,然后找出最大的那个。也没啥好办法,因为这些坐标点也并不是分布在坐标轴上。只能使用三角形公式求解。
实现代码:
class Solution {
public double largestTriangleArea(int[][] points) {
double res = -1;
for(int []m:points)
for(int []n:points)
for(int []k:points){
double temp = 0.5*Math.abs(m[0]*(n[1]-k[1]) + n[0]*(k[1]-m[1]) + k[0]*(m[1]-n[1]));
res = Math.max(res,temp);
}
return res;
}
}