LeetCode Entry

812. Largest Triangle Area

27.09.2025 easy 2025 kotlin rust

Max area triangle

812. Largest Triangle Area easy blog post substack youtube

1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1125

Problem TLDR

Max area triangle #easy

Intuition

Brute-force & Google for formula.

Approach

  • max are triangle lies on a convex-hull

Complexity

  • Time complexity: \(O(n^2log(n))\) or n^2

  • Space complexity: \(O(1)\)

Code


// 31ms
    fun largestTriangleArea(p: Array<IntArray>) =
        p.maxOf {(x1,y1)-> p.maxOf{(x2,y2)-> p.maxOf{(x3,y3) ->
        abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)) }}} * 0.5


// 3ms
    pub fn largest_triangle_area(p: Vec<Vec<i32>>) -> f64 {
        let mut r = 0f64;
        for p1 in &p { for p2 in &p { for p3 in &p {
            r = r.max(((p2[0]-p1[0])*(p3[1]-p1[1])-(p3[0]-p1[0])*(p2[1]-p1[1])).abs()as f64)
        }}}; r * 0.5
    }