Skip to content

Commit ae7e701

Browse files
Merge pull request #305 from tiennguyen2310/patch-90
Create LargestTriangleArea.java
2 parents f17d32e + a46c7e3 commit ae7e701

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
812. Largest Triangle Area
3+
Solved
4+
Easy
5+
Topics
6+
premium lock icon
7+
Companies
8+
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.
9+
10+
11+
12+
Example 1:
13+
14+
15+
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
16+
Output: 2.00000
17+
Explanation: The five points are shown in the above figure. The red triangle is the largest.
18+
Example 2:
19+
20+
Input: points = [[1,0],[0,0],[0,1]]
21+
Output: 0.50000
22+
23+
24+
Constraints:
25+
26+
3 <= points.length <= 50
27+
-50 <= xi, yi <= 50
28+
All the given points are unique.
29+
*/
30+
class Solution {
31+
private int doubleArea(int[] a, int[] b, int[] c){
32+
//return AB x AC
33+
int res = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
34+
return Math.abs(res);
35+
}
36+
public double largestTriangleArea(int[][] points) {
37+
int res = 0;
38+
for(int i = 0; i < points.length; i++)
39+
for(int j = i + 1; j < points.length; j++)
40+
for(int k = j + 1; k < points.length; k++)
41+
res = Math.max(res, doubleArea(points[i], points[j], points[k]));
42+
43+
double ans = (double)(res) / 2.0;
44+
return ans;
45+
}
46+
}

0 commit comments

Comments
 (0)