KatInterview

830. Largest Triangle Area

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the area of the largest triangle that can be formed by any three points from a given list of points on a 2D plane. The number of points, n, is small, with 3 <= n <= 50. This constraint suggests that a brute-force approach, which checks every possible combination of three points, is computationally feasible.

Solution

Largest Triangle Area in Python

class Solution:
    def largestTriangleArea(self, points: list[list[int]]) -> float:
        n = len(points)
        max_area = 0.0
        
        # Iterate through all possible combinations of three points
        for i in range(n):
            for j in range(i + 1, n):
                for k in range(j + 1, n):
                    # Coordinates of the three points
                    x1, y1 = points[i]
                    x2, y2 = points[j]
                    x3, y3 = points[k]
                    
                    # Calculate area using the Shoelace formula
                    # Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|
                    current_area = 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
                    
                    # Update the maximum area
                    if current_area > max_area:
                        max_area = current_area
                        
        return max_area

Largest Triangle Area in Java

class Solution {
    public double largestTriangleArea(int[][] points) {
        int n = points.length;
        double maxArea = 0.0;

        // Iterate through all possible combinations of three points
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int k = j + 1; k < n; k++) {
                    // Coordinates of the three points
                    int x1 = points[i][0], y1 = points[i][1];
                    int x2 = points[j][0], y2 = points[j][1];
                    int x3 = points[k][0], y3 = points[k][1];
                    
                    // Calculate area using the Shoelace formula
                    // Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|
                    // Multiplying by 0.5 promotes the entire expression to double for precision.
                    double currentArea = 0.5 * Math.abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
                    
                    // Update the maximum area
                    if (currentArea > maxArea) {
                        maxArea = currentArea;
                    }
                }
            }
        }
        
        return maxArea;
    }
}

Largest Triangle Area in C++

class Solution {
public:
    double largestTriangleArea(vector<vector<int>>& points) {
        int n = points.size();
        double max_area = 0.0;
        
        // Iterate through all possible combinations of three points
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                for (int k = j + 1; k < n; ++k) {
                    // Coordinates of the three points.
                    // Cast to double for calculation to maintain precision.
                    double x1 = points[i][0];
                    double y1 = points[i][1];
                    double x2 = points[j][0];
                    double y2 = points[j][1];
                    double x3 = points[k][0];
                    double y3 = points[k][1];
                    
                    // Calculate area using the Shoelace formula
                    // Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|
                    double current_area = 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
                    
                    // Update the maximum area
                    if (current_area > max_area) {
                        max_area = current_area;
                    }
                }
            }
        }
        
        return max_area;
    }
};

Largest Triangle Area in TypeScript

function largestTriangleArea(points: number[][]): number {
    const n = points.length;
    let maxArea = 0.0;

    // Iterate through all possible combinations of three points
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            for (let k = j + 1; k < n; k++) {
                // Coordinates of the three points using array destructuring
                const [x1, y1] = points[i];
                const [x2, y2] = points[j];
                const [x3, y3] = points[k];

                // Calculate area using the Shoelace formula
                // Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)|
                const currentArea = 0.5 * Math.abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));

                // Update the maximum area
                if (currentArea > maxArea) {
                    maxArea = currentArea;
                }
            }
        }
    }

    return maxArea;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.