-
题目
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.
Example: Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] Output: 2 Explanation: The five points are show in the figure below. The red triangle is the largest.
-
题目大意&解题思路
给你一些二维坐标,让你求出其中三个坐标组成的三角形中最大的三角形面积。
其实没有别的好的方法就是暴力解法,挨个算,然后求最大的。
平面中三个点(x1,y1)(x2,y2) (x3,y3)组成的三角形的面积公式为:S = (1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2)
-
解题方法
double largestTriangleArea(vector<vector<int>>& points) {
double max_area = 0, area;
int n = points.size();
for( int i = 0; i < n - 2; ++i ){
for( int j = i + 1; j < n - 1; ++j ){
for( int k = j + 1; k < n; ++k ){
area = abs(points[i][0]*points[j][1] + points[j[0]*points[k][1]
+ points[k][0]*points[i][1] - points[i][0]*points[k][1] -
points[j][0]*points[i][1] - points[k][0]*points[j][1]) / 2;
max_area = max( max_area, area);
}
}
}
return max_area;
}
-
结果