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. javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/websocket/{sid}] : existing endpoint was class com.sanyi.qibaobusiness.framework.webSocket.WebSocketServe

    报错: javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/w ...

  2. 《11招玩转网络安全》之第一招:Docker For Docker

    玩转黑客那些工具,缺少了虚拟机怎么行,除了用虚拟机虚拟整个系统,Docker也不能缺少,读者只需要知道,Docker只虚拟Linux系统中的某个程序就可以了.本节就来介绍Linux下安装设置Docke ...

  3. volative 与处理器的嗅探技术

    在<java并发编程的艺术>这本书中,关于volatile的内存原理本质的描述如下: 有volatile变量修饰共享变量在编译器编译后,后多出一个“lock” 来(lock前缀指令相当于一 ...

  4. Contest2156 - 2019-3-7 高一noip基础知识点 测试2 题解版

    传送门 预计得分:100+70+100+50=320 实际得分100+63+77+30=270 Ctrl_C+Ctrl_V时不要粘贴翻译的,直接粘原文, In a single line of the ...

  5. How to avoid the 0-byte file on Webclient download error

    _client.DownloadDataAsync(new Uri(url)); _client.DownloadDataCompleted += (sender, e) => { try { ...

  6. 通俗易懂的vuex-demo

    在main.js引入store.js

  7. 20165237 2017-2018-2《Java程序设计》课程总结

    20165237 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 我期望的师生关系 学习基础和C语言基础调查 Linux安装及学习 第一周学习总结 第二周学习总结 第 ...

  8. Windows 快捷键总结

    一.日常用法 1.Win + R 运行命令窗,cmd等必备,令人进入注册表,也需要通过运行命令窗,如注册表示运行 Regedit 命令,组策略使用 gpedit.msc 命令等. 2.Win + E ...

  9. 【转】【Linux】Swap与Memory

    背景介绍 Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级.所以,程序和数据如果在内存的话,会有非常快的读写速度.但是,内存的造价是要高于磁盘的,且内存的断电丢失数据也 ...

  10. 入坑C++之vs 新建C++项目

    首先呢,下载一个visual  studio 2017的编辑器工具,可能有最新的2019年的版本,根据自己喜欢选择哪个版本就好了 visual studio IDE下载地址:https://visua ...