leetcode-812-Largest Triangle Area
题目描述:
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^-6of 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的更多相关文章
- 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 ...
- 【Leetcode_easy】812. Largest Triangle Area
problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...
- 【LeetCode】812. Largest Triangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 三重循环 组合函数 日期 题目地址:https:// ...
- 812. Largest Triangle Area
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [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 ...
- [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 ...
- Leetcode812.Largest Triangle Area最大三角形面积
给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积. 示例: 输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] 输出: 2 解释: 这 ...
- Largest Rectangular Area in a Histogram
题目地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ ,刚開始事实上没做这个题,而是在做https://oj.le ...
- 2018 ICPC Asia Singapore Regional A. Largest Triangle (计算几何)
题目链接:Kattis - largesttriangle Description Given \(N\) points on a \(2\)-dimensional space, determine ...
随机推荐
- 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值
[抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
- hadoop组件启动和关闭命令
一.启动相关组件之前 一般安装完hadoop之后需要格式化一遍hdfs: hdfs namenode -format 然后再进行其他组件的启动,hadoop相关组件都是用位于...hadoop/sbi ...
- Red Hat Cluster Suite 组件 fencing FAQ
说明 Red Hat Cluster实现HA的关键组件之一是fencing.没有设置fencing,虽然看上去也能够运行Cluster,但是一旦遇到故障切换就会出现异 常,所以深入理解fencing原 ...
- asp.net 母版页使用详解
母版页是VS2005中新引入的一个概念,它很好地实现界面设计的模块化,并且实现实现了代码的重用.它就像婚纱影楼中的婚纱模板,同一个婚纱模板可以给不同的新人用,只要把他们的照片贴在已有的婚纱模板就可以形 ...
- Linux arp命令
一.简介 arp命令用于操作主机的arp缓冲区,可以用来显示arp缓冲区中的所有条目.删除指定的条目或者添加静态的ip地址与MAC地址对应关系. 二.语法 -a<主机>:显示arp缓冲区的 ...
- 三个参数,对mysql存储限制的影响
1.max_allowed_packet 这个参数会影响单此插入或读取的包的大小,一般和blob字段共用,但要注意一点是这个参数好像是分服务端与客户端的,如果想输出大字段的内容,则在用客户端链接服务 ...
- java读取properties配置文件[转]
网上文章常见的几种读取.properties文件的方式 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInp ...
- Zedboard学习(三):PL下流水灯实验 标签: fpgazynqPL 2017-07-05 11:09 21人阅读 评论(0)
zynq系列FPGA分为PS部分和PL部分. PL: 可编程逻辑 (Progarmmable Logic), 就是FPGA部分. PS: 处理系统 (Processing System) , 就是与F ...
- Ubuntu 安装phpmyadmin 和配置
ubuntu 安装 phpmyadmin 两种 : 1: apt-get 安装 然后使用 已有的虚拟主机目录建立软连接 sudo apt-get install phpmyadmin sud ...
- aspnetcore的那些actionresult们
比MVC5多了n个actionresult,傻傻分不清,整理了下,妈妈再也不用担心了 https://docs.asp.net/projects/api/en/latest/autoapi/Micro ...