LeetCode 812 Largest Triangle Area 解题报告
题目要求
You have a list of points in the plane. Return the area of the largest triangle that can be formed by any 3 of the points.
题目分析及思路
给定一个平面上的一组点,要求得到由这些点中任意三个所能组成最大面积的三角形。可以对这一组点进行三三组合,然后利用已知三点求三角形面积的公式得到每一组点所对应的三角形面积,最后取其中的最大值即为所求。
python代码
class Solution:
def largestTriangleArea(self, points: List[List[int]]) -> float:
def area(p, q, r):
return .5 * abs(p[0]*q[1] - p[1]*q[0] + q[0]*r[1] - q[1]*r[0] + r[0]*p[1] - r[1]*p[0])
return max(area(*tri_points) for tri_points in itertools.combinations(points, 3))