Skip to content

Commit 4d4893d

Browse files
Merge pull request SharingSource#518 from SharingSource/ac_oier
✨feat: Add 812
2 parents 1e9ff45 + a163b93 commit 4d4893d

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
| [796. 旋转字符串](https://leetcode-cn.com/problems/rotate-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-string/solution/by-ac_oier-bnkx/) | 简单 | 🤩🤩🤩 |
9393
| [804. 唯一摩尔斯密码词](https://leetcode-cn.com/problems/unique-morse-code-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/unique-morse-code-words/solution/by-ac_oier-a9hv/) | 简单 | 🤩🤩🤩 |
9494
| [806. 写字符串需要的行数](https://leetcode-cn.com/problems/number-of-lines-to-write-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-lines-to-write-string/solution/by-ac_oier-5hg2/) | 简单 | 🤩🤩🤩🤩 |
95+
| [812. 最大三角形面积](https://leetcode.cn/problems/largest-triangle-area/) | [LeetCode 题解链接](https://leetcode.cn/problems/largest-triangle-area/solution/by-ac_oier-htv8/) | 简单 | 🤩🤩🤩🤩 |
9596
| [819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/most-common-word/solution/by-ac_oier-6aqd/) | 简单 | 🤩🤩🤩🤩 |
9697
| [821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/) | 简单 | 🤩🤩🤩🤩 |
9798
| [824. 山羊拉丁文](https://leetcode-cn.com/problems/goat-latin/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/goat-latin/solution/by-ac_oier-t7hj/) | 简单 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[812. 最大三角形面积](https://leetcode.cn/problems/largest-triangle-area/solution/by-ac_oier-htv8/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积。
10+
11+
示例:
12+
```
13+
输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
14+
15+
输出: 2
16+
17+
解释:
18+
这五个点如下图所示。组成的橙色三角形是最大的,面积为2。
19+
```
20+
21+
注意:
22+
* $3 <= points.length <= 50.$
23+
* 不存在重复的点。
24+
* $-50 <= points[i][j] <= 50$
25+
* 结果误差值在 $10^{-6}$ 以内都认为是正确答案。
26+
27+
---
28+
29+
### 模拟
30+
31+
根据题意模拟即可:枚举三角形三个顶点并计算面积。
32+
33+
代码:
34+
```Java
35+
class Solution {
36+
public double largestTriangleArea(int[][] ps) {
37+
int n = ps.length;
38+
double ans = 0;
39+
for (int i = 0; i < n; i++) {
40+
for (int j = i + 1; j < n; j++) {
41+
for (int k = j + 1; k < n; k++) {
42+
int cur = cross(ps[j][0] - ps[i][0], ps[j][1] - ps[i][1], ps[k][0] - ps[i][0], ps[k][1] - ps[i][1]);
43+
ans = Math.max(ans, Math.abs(cur / 2.0));
44+
}
45+
}
46+
}
47+
return ans;
48+
}
49+
int cross(int a, int b, int c, int d) {
50+
return a * d - c * b;
51+
}
52+
}
53+
```
54+
* 时间复杂度:$O(n^3)$
55+
* 空间复杂度:$O(1)$
56+
57+
---
58+
59+
### 最后
60+
61+
这是我们「刷穿 LeetCode」系列文章的第 `No.812` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
62+
63+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
64+
65+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
66+
67+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
68+

0 commit comments

Comments
 (0)