LeetCode 812. Largest Triangle Area
考点 | 难度 |
---|---|
Array | Easy |
题目
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.
思路
把每一种选择组成的三角形面积都算一遍,选最大的。三角形面积的公式算法:https://www.mathopenref.com/coordtrianglearea.html
答案
public double largestTriangleArea(int[][] p) {double res = 0;for (int[] i: p)for (int[] j: p)for (int[] k: p)res = Math.max(res, 0.5 * Math.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]));return res;
}