From a46c7e3ffdc17fed841f760fea811aed71a6c313 Mon Sep 17 00:00:00 2001 From: Tien Nguyen <95487001+tiennguyen2310@users.noreply.github.com> Date: Fri, 26 Sep 2025 22:53:38 -0400 Subject: [PATCH] Create LargestTriangleArea.java --- .../leetcode/LargestTriangleArea.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/codingquestions/leetcode/LargestTriangleArea.java diff --git a/src/codingquestions/leetcode/LargestTriangleArea.java b/src/codingquestions/leetcode/LargestTriangleArea.java new file mode 100644 index 00000000..5e86fdd3 --- /dev/null +++ b/src/codingquestions/leetcode/LargestTriangleArea.java @@ -0,0 +1,46 @@ +/* +812. Largest Triangle Area +Solved +Easy +Topics +premium lock icon +Companies +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 + + +Constraints: + +3 <= points.length <= 50 +-50 <= xi, yi <= 50 +All the given points are unique. +*/ +class Solution { + private int doubleArea(int[] a, int[] b, int[] c){ + //return AB x AC + int res = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); + return Math.abs(res); + } + public double largestTriangleArea(int[][] points) { + int res = 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++) + res = Math.max(res, doubleArea(points[i], points[j], points[k])); + + double ans = (double)(res) / 2.0; + return ans; + } +}