812. Largest Triangle Area
A = (1/2) [x1 (y2 – y3 ) + x2 (y3 – y1 ) + x3(y1 – y2)]
T: O(n^3)
```java
class Solution {
public double largestTriangleArea(int[][] points) {
double result = 0;
for (int i = 0; i < points.length; i++) {
for (int j = i+1; j < points.length; j++) {
for (int k = j+1; k < points.length; k++) {
result = Math.max(result, helper(points[i], points[j], points[k]));
}
}
}
return result;
}
private double helper(int num1[], int num2[], int num3[]) {
double x1 = (double) num1[0];
double y1 = (double) num1[1];
double x2 = (double) num2[0];
double y2 = (double) num2[1];
double x3 = (double) num3[0];
double y3 = (double) num3[1];
return Math.abs((x1*(y2-y3) + x2*(y3 - y1) + x3*(y1 - y2)))/2.0;
}
}
/**
who knows it, fuck
using Shoelace's Formula (also known as Gauss's area formula and surveyor's formula):
Area = 0.5 * |x1*(y2-y3) + x2*(y3-y1) + x3(y1-y2)|
*/
```
public double largestTriangleArea(int[][] p) {
double res = 0;
for (int[] i: p)
for (int[] j: p)
for (int[] k: p)
res = Math.max(res, 0.5 * Math.abs(
i[0] * j[1] + j[0] * k[1] + k[0] * i[1]
- j[0] * i[1] - k[0] * j[1] - i[0] * k[1])
);
return res;
}
Last updated