1,题目要求
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.
给出一系列的坐标点,计算这些坐标点所能构成的最大的三角形的面积。
2,题目思路
首先,因为这些坐标点并不是只分布在坐标轴上的,因此在计算面积时需要直接使用常规的坐标三点平面三角计算公式。
而这三个点如何选择,并没有太好的解决方案,用穷举法相对来说是一种可解的方案。
3,程序源码
class Solution {
public:
double largestTriangleArea(vector<vector<int>>& points) {
double res = -1;
for(auto m : points)
for(auto n : points)
for(auto p : points)
{
double tmpArea = 0.5 * abs(m[0]*n[1] + n[0]*p[1] + p[0]*m[1] - m[0]*p[1] - n[0]*m[1] - p[0]*n[1]);
res = max(res, tmpArea);
}
return res;
}
};
注:
在二维的向量中,对于其中的一个分量是一维向量,因此m[0]表示第一个元素,m[1]表示第二个元素。