题目描述:

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.

Notes:

  • 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.

要完成的函数:

double largestTriangleArea(vector<vector<int>>& points)

说明:

1、这道题给定所有点的坐标,要在这些点中间构建一个面积最大的三角形,最后返回这个三角形的面积。

2、这道题最开始想着,能不能直接找到这三个点,最后返回面积就好了。

但很快就发现,通过寻找距离圆心最远的点 i ,可以找到这个面积最大的三角形中的一个点。

但其余两个点就不知道能怎样找到。距离点 i 最远的一个点作为第二个点?好像不太对。

最后还是在暴力法下屈服了……

三角形的面积公式是:

已知三个点为(x1,y1),(x2,y2),(x3,y3)

面积为A= 1/2 * [ x1(y2-y3) + x2(y3-y1) + x3(y1-y2) ]

代码如下,分享给大家:

    double largestTriangleArea(vector<vector<int>>& points)
{
int s1=points.size();
double res=0;
double area;
for(int i=0;i<s1;i++)
{
for(int j=i+1;j<s1;j++)
{
for(int k=j+1;k<s1;k++)
{
area=0.5*abs(points[i][0]*(points[j][1]-points[k][1])+points[j][0]*(points[k][1]-points[i][1])+points[k][0]*(points[i][1]-points[j][1]));
res=max(res,area);
}
}
}
return res;
}

上述代码时间复杂度为O(n^3),实测7ms,没有百分比,因为网站提示当前提交量还不够多。

leetcode-812-Largest Triangle Area的更多相关文章

  1. LeetCode 812 Largest Triangle Area 解题报告

    题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...

  2. 【Leetcode_easy】812. Largest Triangle Area

    problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...

  3. 【LeetCode】812. Largest Triangle Area 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 三重循环 组合函数 日期 题目地址:https:// ...

  4. 812. Largest Triangle Area

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  5. [LeetCode] Largest Triangle Area 最大的三角区域

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  6. [Swift]LeetCode812. 最大三角形面积 | Largest Triangle Area

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  7. Leetcode812.Largest Triangle Area最大三角形面积

    给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积. 示例: 输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] 输出: 2 解释: 这 ...

  8. Largest Rectangular Area in a Histogram

    题目地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ ,刚開始事实上没做这个题,而是在做https://oj.le ...

  9. 2018 ICPC Asia Singapore Regional A. Largest Triangle (计算几何)

    题目链接:Kattis - largesttriangle Description Given \(N\) points on a \(2\)-dimensional space, determine ...

随机推荐

  1. c++ 中介者模式(mediator)

    中介者模式:用一个中介对象来封装一系列的对象交互.中介者使各个对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互.中介者模式的例子很多,大到联合国安理会,小到房屋中介.下面以 ...

  2. Java故障分析基础

    JVM基础 垃圾回收器 GC日志 jps, jinfo命令 jmap, jhat命令 jstat命令 线程dump jvisualVM / jconsole MAT(Memory Analyzer t ...

  3. SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、yml属性配置、多环境配置、自定义properties配置

    1 IntelliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到 ...

  4. winfrom 循环播放图片

    没啥新东西了,就是遍历和匹配文件名然后获取对象,放到picturebox里面 选中listview中想要查看的图片,然后点击查看按钮,进行↓代码. if (listView1.SelectedItem ...

  5. 303. Range Sum Query 范围求和系列

    Immutable [抄题]: Given an integer array nums, find the sum of the elements between indices i and j (i ...

  6. Jenkins持续集成构建

    配置Sonar.Jenkins进行持续审查 http://go2live.cn/archives/38261.html Jenkins iOS 项目持续集成 http://go2live.cn/arc ...

  7. jquery简单ajax示例_读取json文件数据

    来自于<jquery权威指南> -------------------------------------- 点击button后,获取到json文件数据,显示如下: Json文件: [ { ...

  8. JSON_UNESCAPED_UNICODE

    JSON_UNESCAPED_UNICODE(中文不转为unicode)

  9. Oracle——序列、索引、同义词

    一.常见的数据库对象 二.序列 序列: 可供多个用户用来产生唯一数值的数据库对象 自动提供唯一的数值 共享对象 主要用于提供主键值 将序列值装入内存可以提高访问效率 ①.创建序列 CREATE SEQ ...

  10. 项目中遇到的死锁问题: Lock wait timeout exceeded; try restarting transaction

    最近项目中频繁出现  Lock wait timeout exceeded; try restarting transaction这个错误,把我们弄得痛苦不堪啊,为了解决问题,上网上找好多资料,终于把 ...