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.

这道题给了我们一系列的二维平面上的点,让我们找出任意三个点能组成的最大三角形的面积。那么我们只能遍历所有的三角形面积,然后找出最大的那个。貌似这道题也没有啥特别简便的方法,不遍历不行啊。遍历任意三个点简单,问题来了,如何通过三个顶点的坐标求出三角形面积,这个可就是初中几何题了,博主也不记得,只能上网搜一波。就是用下面这个公式即可:

这里面三个顶点分别是(x1, y1),(x2, y2),(x3, y3),有了公式后,本题就没有什么难点了,参见代码如下:

解法一:

class Solution {
public:
double largestTriangleArea(vector<vector<int>>& points) {
double res = ;
for (int i = ; i < points.size(); ++i) {
for (int j = i + ; j < points.size(); ++j) {
for (int k = j + ; k < points.size(); ++k) {
int x1 = points[i][], y1 = points[i][];
int x2 = points[j][], y2 = points[j][];
int x3 = points[k][], y3 = points[k][];
double area = abs(0.5 * (x2 * y3 + x1 * y2 + x3 * y1 - x3 * y2 - x2 * y1 - x1 * y3));
res = max(res, area);
}
}
}
return res;
}
};

我们也可以稍稍简化一下上面的写法,但是解题思路没有任何区别,参见代码如下:

解法二:

class Solution {
public:
double largestTriangleArea(vector<vector<int>>& points) {
double res = ;
for (auto &i : points) {
for (auto &j : points) {
for (auto &k : points) {
res = max(res, 0.5 * abs(i[] * j[] + j[] * k[] + k[] * i[]- j[] * i[] - k[] * j[] - i[] * k[]));
}
}
}
return res;
}
};

参考资料:

https://leetcode.com/problems/largest-triangle-area/discuss/122711/C++JavaPython-Solution-with-Explanation-and-Prove

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Largest Triangle Area 最大的三角区域的更多相关文章

  1. 【Leetcode_easy】812. Largest Triangle Area

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

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

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

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

  4. [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 ...

  5. 812. Largest Triangle Area

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

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

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

  7. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  8. Largest Rectangular Area in a Histogram

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

  9. leetcode-812-Largest Triangle Area

    题目描述: You have a list of points in the plane. Return the area of the largest triangle that can be fo ...

随机推荐

  1. $L^p$ 调和函数恒为零

    设 $u$ 是 $\bbR^n$ 上的调和函数, 且 $$\bex \sen{u}_{L^p}=\sex{\int_{\bbR^n}|u(y)|^p\rd y}^{1/p}<\infty. \e ...

  2. win10自带邮箱添加网易企业邮箱

    开始-邮件-账户-添加账户-高级安装程序-internet电子邮件-然后输入网易企业邮箱的用户名和相关服务器设置就行了 接收服务器 pop.qiye.163.com发送服务器 smtp.qiye.16 ...

  3. StringBuffer/StringBuilder总结

  4. codeblocks1712设置中文

    下载汉化包:百度网盘,密码:7rrz 下载后放到安装目录:D:\Program Files (x86)\CodeBlocks\share\CodeBlocks\locale\zh_CN(根据个人安装目 ...

  5. 【洛谷P2660烤鸡】

    题目背景 猪猪hanke得到了一只鸡 题目描述 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10种配料(芥末.孜然等),每种配料可以放1—3克, ...

  6. Django之auth模块

    http://www.cnblogs.com/liwenzhou/p/9030211.html 1.首先导入auth模块 from django.contrib import auth 2.创建aut ...

  7. 递归 - Leetcode 110 判断二叉树是否为平衡二叉树

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  8. php的phar是什么?

    phar 要求5.2以上 前言 最近在看composer,是下载了一个composer.phar,然后放到/usr/local/bin目录下,就可以全局使用composer了,然而并不懂phar是什么 ...

  9. Lua中的模块与包

    [前言] 从Lua5.1版本开始,就对模块和包添加了新的支持,可是使用require和module来定义和使用模块和包.require用于使用模块,module用于创建模块.简单的说,一个模块就是一个 ...

  10. Mockito框架入门教程(一)

    官网: http://mockito.org API文档:http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html 项目源码:htt ...