Skip to content

Commit 99e0525

Browse files
add 812
1 parent 58f4bfa commit 99e0525

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ _If you like this project, please leave me a star._ ★
488488
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_819.java) | |Easy| HashMap
489489
|816|[Ambiguous Coordinates](https://leetcode.com/problems/ambiguous-coordinates/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_816.java) | |Medium| String
490490
|814|[Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_814.java) | |Medium| recursion, DFS
491+
|812|[Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_812.java) | |Easy| Array, Math, Geometry
491492
|811|[Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_811.java) | |Easy| HashMap
492493
|809|[Expressive Words](https://leetcode.com/problems/expressive-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_809.java) | |Medium|
493494
|807|[Max Increase to Keep City Skyline](https://leetcode.com/problems/max-increase-to-keep-city-skyline/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_807.java) | |Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _812 {
4+
public static class Solution1 {
5+
/**
6+
* reference: https://www.mathopenref.com/coordtrianglearea.html
7+
*/
8+
public double largestTriangleArea(int[][] points) {
9+
double largestArea = 0.0;
10+
for (int i = 0; i < points.length - 2; i++) {
11+
for (int j = i + 1; j < points.length - 1; j++) {
12+
for (int k = j + 1; k < points.length; k++) {
13+
double area = Math.abs(points[i][0] * (points[j][1] - points[k][1]) + points[j][0] * (points[k][1] - points[i][1]) + points[k][0] * (points[i][1] - points[j][1])) / 2.0;
14+
largestArea = Math.max(largestArea, area);
15+
16+
}
17+
}
18+
}
19+
return largestArea;
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._812;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _812Test {
11+
private static _812.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _812.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(2, solution1.largestTriangleArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,0],[0,1],[1,0],[0,2],[2,0]")), 0.0000001);
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(1799.0, solution1.largestTriangleArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[-35,19],[40,19],[27,-20],[35,-3],[44,20],[22,-21],[35,33],[-19,42],[11,47],[11,37]")), 0.0000001);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)