Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

812. Largest Triangle Area - Leetcode Solution

Problem Description

Given a list of points on the 2D plane, each represented as [x, y], your task is to find the largest possible area of a triangle that can be formed by any three of these points. You must select exactly three different points for each triangle, and you cannot reuse points within the same triangle. The input is a list of integer coordinate pairs, and you should return the maximum area (as a float) that can be achieved by any such triangle.

  • Input: points - a list of n points, where each point is a list [x, y].
  • Output: The largest triangle area (as a float, not necessarily integer).
  • Constraints: 3 <= n <= 50, -50 <= x, y <= 50.
  • Every triangle must use three distinct points from the list.

Thought Process

The problem asks for the largest area of a triangle that can be formed from a given set of points. The most direct approach is to consider every possible combination of three points, calculate the area for each triangle, and keep track of the maximum found.

Since the number of points is small (at most 50), a brute-force solution that checks all possible triangles is feasible. However, it is still important to use an efficient formula for calculating the area of a triangle given its vertices. The Shoelace formula (or the cross product) allows us to compute the area directly from the coordinates without needing to compute distances or angles.

We should also consider edge cases, such as when three points are collinear, resulting in zero area.

Solution Approach

The solution consists of the following steps:

  1. Iterate over all possible combinations: Use three nested loops (or a combinations function) to generate every possible selection of three distinct points from the input list.
  2. Calculate the triangle area: For each triplet, use the Shoelace formula:
    • Given points A(x1, y1), B(x2, y2), C(x3, y3), the area is 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|.
  3. Track the largest area: Keep a variable to store the maximum area found so far, and update it whenever a larger area is found.
  4. Return the result: After all combinations have been checked, return the largest area found.

This approach is simple, direct, and leverages the fact that the input size is small enough to allow checking all possible triangles.

Example Walkthrough

Let's consider the input points = [[0,0], [0,1], [1,0], [0,2], [2,0]].

  1. We generate all possible combinations of three points. For example, let's check [0,0], [0,1], [1,0]:
    • Area = 0.5 * |0*(1-0) + 0*(0-0) + 1*(0-1)| = 0.5 * |0 + 0 -1| = 0.5 * 1 = 0.5
  2. Next, try [0,0], [0,2], [2,0]:
    • Area = 0.5 * |0*(2-0) + 0*(0-0) + 2*(0-2)| = 0.5 * |0 + 0 -4| = 0.5 * 4 = 2.0
  3. Continue this process for all combinations. The largest area found in this example is 2.0.

Thus, the function should return 2.0 for this input.

Time and Space Complexity

  • Brute-force approach:
    • There are C(n, 3) = n*(n-1)*(n-2)/6 possible triangles to check. For n=50, this is 19,600 combinations.
    • For each combination, area calculation is O(1).
    • Total time complexity: O(n^3).
    • Space complexity: O(1), since we only need a variable to track the maximum area.
  • Optimized approaches: For this problem, further optimization is unnecessary due to the small input size and the O(1) area calculation.

Code Implementation

from itertools import combinations

class Solution:
    def largestTriangleArea(self, points):
        def area(a, b, c):
            return abs(
                a[0]*(b[1]-c[1]) +
                b[0]*(c[1]-a[1]) +
                c[0]*(a[1]-b[1])
            ) / 2.0

        max_area = 0.0
        for a, b, c in combinations(points, 3):
            max_area = max(max_area, area(a, b, c))
        return max_area
      
class Solution {
public:
    double largestTriangleArea(vector<vector<int>>& points) {
        double maxArea = 0.0;
        int n = points.size();
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                for (int k = j + 1; k < n; ++k) {
                    double area = 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;
                    if (area > maxArea)
                        maxArea = area;
                }
            }
        }
        return maxArea;
    }
};
      
class Solution {
    public double largestTriangleArea(int[][] points) {
        double maxArea = 0.0;
        int n = points.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int k = j + 1; k < n; k++) {
                    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;
                    if (area > maxArea)
                        maxArea = area;
                }
            }
        }
        return maxArea;
    }
}
      
var largestTriangleArea = function(points) {
    let maxArea = 0.0;
    let n = points.length;
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            for (let k = j + 1; k < n; k++) {
                let 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;
                if (area > maxArea)
                    maxArea = area;
            }
        }
    }
    return maxArea;
};
      

Summary

The largest triangle area problem can be efficiently solved by brute-force thanks to the small input size. The Shoelace formula provides a fast, direct way to compute triangle areas from coordinates. By systematically checking every possible triangle, we guarantee finding the maximum area, and the solution is easy to implement in any major programming language. This approach is both simple and robust for this constraint set, demonstrating the power of mathematical formulas and exhaustive search when used appropriately.