File: largest-triangle-area/solution.py

Date: 2026-06-06

Time: 17:18

largest-triangle-area/solution.py

Purpose

This file solves LeetCode 812 — Largest Triangle Area. Given a list of 2D points, it finds the maximum area among all triangles formed by any three of those points. It's a brute-force combinatorial geometry solution.

Key Components

Solution.largestTriangleArea(self, points: list[list[int]]) -> float

The single method. Takes a list of [x, y] coordinate pairs and returns the largest triangle area as a float.

The area computation uses the Shoelace formula (a.k.a. the surveyor's formula), which computes a triangle's area from vertex coordinates without needing side lengths or angles:


area = |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)| / 2

This is equivalent to half the absolute value of the cross product of two edge vectors, which gives the area of the parallelogram they span.

Patterns

Dependencies

Flow

1. Initialize max_area = 0.0.

2. For each triple of points from combinations(points, 3):

3. Return max_area.

Invariants

Error Handling

None. The function trusts its input matches the LeetCode contract (at least 3 points, integer coordinates). Passing fewer than 3 points would cause combinations to yield nothing, and the function would silently return 0.0.