812. Largest Triangle Area LeetCode Solution

In this guide, you will get 812. Largest Triangle Area LeetCode Solution with the best time and space complexity. The solution to Largest Triangle Area problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Largest Triangle Area solution in C++
  4. Largest Triangle Area solution in Java
  5. Largest Triangle Area solution in Python
  6. Additional Resources
812. Largest Triangle Area LeetCode Solution image

Problem Statement of 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

Constraints:

3 <= points.length <= 50
-50 <= xi, yi <= 50
All the given points are unique.

Complexity Analysis

  • Time Complexity: O(n^3)
  • Space Complexity: O(1)

812. Largest Triangle Area LeetCode Solution in C++

class Solution {
 public:
  double largestTriangleArea(vector<vector<int>>& points) {
    double ans = 0;

    for (const vector<int>& A : points)
      for (const vector<int>& B : points)
        for (const vector<int>& C : points)
          ans = max(ans, 0.5 * abs((B[0] - A[0]) * (C[1] - A[1]) -
                                   (C[0] - A[0]) * (B[1] - A[1])));

    return ans;
  }
};
/* code provided by PROGIEZ */

812. Largest Triangle Area LeetCode Solution in Java

class Solution {
  public double largestTriangleArea(int[][] points) {
    double ans = 0;

    for (int[] A : points)
      for (int[] B : points)
        for (int[] C : points)
          ans = Math.max(ans, 0.5 * Math.abs((B[0] - A[0]) * (C[1] - A[1]) - //
                                             (C[0] - A[0]) * (B[1] - A[1])));

    return ans;
  }
}
// code provided by PROGIEZ

812. Largest Triangle Area LeetCode Solution in Python

class Solution:
  def largestTriangleArea(self, points: list[list[int]]) -> float:
    ans = 0

    for Ax, Ay in points:
      for Bx, By in points:
        for Cx, Cy in points:
          ans = max(ans, 0.5 * abs((Bx - Ax) * (Cy - Ay) -
                                   (Cx - Ax) * (By - Ay)))

    return ans
# code by PROGIEZ

Additional Resources

See also  659. Split Array into Consecutive Subsequences LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.