812-Largest Triangle Area

本文介绍了一种求解平面上一系列点中能构成的最大三角形面积的方法。通过遍历所有可能的三点组合,利用坐标计算三角形面积,并找出最大值。适用于3到50个不重复的点,坐标范围在-50到50之间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description:
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.

这里写图片描述


Note:

  • 3 <= points.length <= 50.
  • No points will be duplicated.
  • 50 <= points[i][j] <= 50.
  • Answers within 10^-6 of the true value will be accepted as correct.

问题描述:
在平面上你有一系列的点,返回可以由任意三个点构成的最大面积。


问题分析:
暴力破解就可以了,注意三点计算面积的公式:
S=12(x1y2+x2y3+x3y1x1y3x2y1x3y2)S=12∗(x1y2+x2y3+x3y1−x1y3−x2y1−x3y2)


解法(brute force):

class Solution {
    public double largestTriangleArea(int[][] points) {
        int len = points.length;
        double maxArea = Integer.MIN_VALUE;

        for(int i = 0;i < len - 2;i++){
            for(int j = i + 1;j < len - 1;j++){
                for(int k = j + 1;k < len;k++){
                    maxArea = Math.max(maxArea, getArea(points[i], points[j], points[k]));
                }
            }
        }

        return maxArea;
    }
    public double getArea(int[] pointi, int[] pointj, int[] pointk){
        return Math.abs(((pointi[0] * pointj[1] + pointj[0] * pointk[1] + pointk[0] * pointi[1] - pointi[0] * pointk[1] - pointj[0] * pointi[1] - pointk[0] * pointj[1]) * 1.0) / 2.0);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值