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) {
int row = points.length;
double max = 0;
for (int i = 0; i < row; ++ i){
for (int j = i + 1; j < row; ++ j){
for (int k = j + 1; k < row; ++ k){
max = Math.max(max, calculateArea(points[i], points[j], points[k]));
}
}
}
return max;
}
public double calculateArea(int[] p, int[] q, int[] r){
return 0.5*Math.abs(p[0]*(q[1] - r[1]) + q[0]*(r[1] - p[1]) + r[0]*(p[1] - q[1]));
}
}