812. Largest Triangle Area

406 篇文章 0 订阅
406 篇文章 0 订阅

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]表示第二个元素。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值