Largest Triangle Area — LeetCode 812 Python Solution
- Problem
- #812
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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
- 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.
Python solution
class Solution:
def largestTriangleArea(self, points: List[List[int]]) -> float:
ans = 0
for x1, y1 in points:
for x2, y2 in points:
for x3, y3 in points:
u1, v1 = x2 - x1, y2 - y1
u2, v2 = x3 - x1, y3 - y1
t = abs(u1 * v2 - u2 * v1) / 2
ans = max(ans, t)
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n^3), where n is the number of points |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 812. Largest Triangle Area is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math and Geometry.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 812. Largest Triangle Area?
- LeetCode 812. Largest Triangle Area is rated Easy on LeetCode.
- What is the time complexity of LeetCode 812. Largest Triangle Area?
- The Python solution on this page runs in O(n^3), where n is the number of points.
- What is the space complexity of LeetCode 812. Largest Triangle Area?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 812. Largest Triangle Area cover?
- LeetCode 812. Largest Triangle Area is tagged Geometry, Array and Math on LeetCode.