-
Notifications
You must be signed in to change notification settings - Fork 0
/
Largest Triangle Area
33 lines (28 loc) · 1.34 KB
/
Largest Triangle Area
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Largest Triangle Area
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.
Example 1:
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
Output: 2.00000
Explanation: The five points are shown in the above figure. The red triangle is the largest.
Example 2:
Input: points = [[1,0],[0,0],[0,1]]
Output: 0.50000
class Solution {
public double largestTriangleArea(int[][] points) {
double result = 0;
for (int x = 0; x < points.length; x++) {
for (int y = x + 1; y < points.length; y++) {
for (int z = y + 1; z < points.length; z++) {
double area = Math.abs((points[x][0] * points[y][1] +
points[y][0] * points[z][1] +
points[z][0] * points[x][1]) -
(points[x][1] * points[y][0] +
points[y][1] * points[z][0] +
points[z][1] * points[x][0])) / (double) 2;
result = Math.max(result, area);
}
}
}
return result;
}
}