aboutsummaryrefslogtreecommitdiff
path: root/largest_triangle_area.py
blob: 506586f93962521fbb0a3ef284b7ab417ea41480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 812. Largest Triangle Area
from itertools import combinations

"""
given an array of points on the x-y plane 'points' where 'points[i] = [x_i,
y_i]' return the area of the largest triangle that can be formed by any three
different points. answers within 10^-5 will be accepted.
"""


class Solution(object):
    def largestTriangleArea(self, points):
        """
        :type points: List[List[int]]
        :rtype: float
        """
        return max(
            0.5
            * abs(
                i[0] * j[1]
                + j[0] * k[1]
                + k[0] * i[1]
                - j[0] * i[1]
                - k[0] * j[1]
                - i[0] * k[1]
            )
            for i, j, k in combinations(points, 3)
        )


if __name__ == "__main__":
    obj = Solution()
    print(obj.largestTriangleArea(points=[[0, 0], [0, 1], [1, 0], [0, 2], [2, 0]]))
    print(obj.largestTriangleArea(points=[[1, 0], [0, 0], [0, 1]]))