普通解法:
无
最优解法:
枚举不同的选择方案,根据一边长度小于另外三边长度之和判断方案是否合法,然后计算其所形成的最大面积,对所有合法方案取最大值即为答案
对于上图四边形,
两边平方,去掉根号可得:
分别在,
中,由余弦定理得:
为了配合上面关于面积的等式,我们将此式构造成下面的式子:
将两式联立,可得:
而
所以:
反复利用平方差公式,可得:,其中
所以有:
当时面积取得最大值。
即
时间复杂度
空间复杂度
#include <cmath>
class Solution {
public:
/**
*
* @param a int整型vector
* @return double浮点型
*/
double calc(int a, int b, int c, int d) {
if (a >= b + c + d) return 0;
if (b >= a + c + d) return 0;
if (c >= a + b + d) return 0;
if (d >= a + b + c) return 0;
double p = (a + b + c + d) / 2.0;
return sqrt((p - a) * (p - b) * (p - c) * (p - d));
}
double solve(vector<int>& a) {
// write code here
double ans = 0;
for (int i = 0; i < a.size(); i++)
for (int j = 0; j < i; j++)
for (int k = 0; k < j; k++)
for (int l = 0; l < k; l++)
ans = max(ans, calc(a[i], a[j], a[k], a[l]));
return ans;
}
}; 


京公网安备 11010502036488号