【LeetCode】812. Largest Triangle Area 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/largest-triangle-area/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.

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.
题目大意
给出一组二维坐标,求这些点能组成的最大三角形面积。
解题方法
三重循环
看了下数据的范围最多到50,所以O(n^3)的时间复杂度肯定能过的。所以直接使用三重遍即可。
根据坐标求三角形面积是有公式的。另外要注意的是我们再求的时候要加上绝对值符号。


class Solution:
def largestTriangleArea(self, points):
"""
:type points: List[List[int]]
:rtype: float
"""
res = 0
N = len(points)
for i in range(N - 2):
for j in range(i + 1, N - 1):
for k in range(i + 2, N):
(x1, y1), (x2, y2), (x3, y3) = points[i], points[j], points[k]
res = max(res, 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)))
return res
组合函数
python的组合公式实现了数学中的组合运算符,节省了代码量。
class Solution:
def largestTriangleArea(self, points):
"""
:type points: List[List[int]]
:rtype: float
"""
# S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2)
def f(p1, p2, p3):
(x1, y1), (x2, y2), (x3, y3) = p1, p2, p3
return 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
return max(f(a, b, c) for a, b, c in itertools.combinations(points, 3))
日期
2018 年 4 月 9 日 —— 天气变好,春暖花开~~
2018 年 11 月 9 日 —— 睡眠可以
【LeetCode】812. Largest Triangle Area 解题报告(Python)的更多相关文章
- 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】223. Rectangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
- 812. Largest Triangle Area
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
随机推荐
- 联盛德 HLK-W806 (六): I2C驱动SSD1306 128x64 OLED液晶屏
目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...
- 开发安卓记账本-HelloAndroid的完成
这个寒假要完成一个家庭记账本软件的开发,今天完成了Android Studio的安装与第一个安卓应用的运行(HelloAndroid) 下图是效果: 1.Android Studio的安装 可直接百度 ...
- A Child's History of England.11
CHAPTER 4 ENGLAND UNDER ATHELSTAN AND THE SIX BOY-KINGS Athelstan, the son of Edward the Elder, succ ...
- 3.0 rust 项目路径
$ rustc --versionrustc 1.44.0 (49cae5576 2020-06-01) 将代码存在到不同的文件 main.rs mod aa; fn main() { println ...
- Spring.DM web开发环境搭建
作为一个初学者来说,搭建好Spring.DM 的web开发环境还是有些麻烦的.我就遇到了N多麻烦,走了很多弯路.本文介绍了2种比较简单的搭建Spring.DM OSGi web开发环境的搭建. 第 ...
- Spring 文档汇总
Spring Batch - Reference Documentation Spring Batch 参考文档中文版 Spring Batch 中文文档 Table 2. JdbcCursorIte ...
- Layui:select下拉框回显
一..需求场景分析 基于Thymeleaf模板下的layui下选框回显. 二.获得一个Layui标配的下拉框,我们需要在html中填写的内容如下 <div class="layui-f ...
- 【Java】面向类与对象
一.面向对象 对象封装:私有变量+公共方法 方法与构造方法 this变量 Animal.java public class Animal { String name; int age; void mo ...
- 【阿菜漏洞复现】DeFi 平台 MonoX Finance 漏洞分析及复现
前言 2021 年 11 ⽉ 30 ⽇,DeFi 平台 MonoX Finance 遭遇攻击,损失共计约 3100 万美元. 造成本次攻击的漏洞主要有两个: 移除流动性的函数未对调用者进行检测,使得任 ...
- Python列表简介和遍历
一.Python3列表简介 1.1.Python列表简介 序列是Python中最基本的数据结构 序列中的每个值都有对应的位置值,称之为索引,第一个索引是0,第二个索引是1,以此类推. Python有6 ...