Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd03a95

Browse files
feat: add solutions to lc problem: No.0812 (doocs#4750)
1 parent e48eff5 commit bd03a95

File tree

4 files changed

+149
-2
lines changed

4 files changed

+149
-2
lines changed

‎solution/0800-0899/0812.Largest Triangle Area/README.md‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### 方法一
56+
### 方法一:枚举三点面积公式
57+
58+
给定平面上三点 $(x_1, y_1),ドル $(x_2, y_2),ドル $(x_3, y_3),ドル其面积公式为:
59+
60+
$$S = \frac{1}{2} \left| x_1y_2 + x_2y_3 + x_3y_1 - x_1y_3 - x_2y_1 - x_3y_2 \right|$$
61+
62+
我们可以枚举所有的三点组合,计算面积的最大值。
63+
64+
时间复杂度 $O(n^3),ドル其中 $n$ 是点的数量。空间复杂度 $O(1)$。
5765

5866
<!-- tabs:start -->
5967

@@ -151,6 +159,53 @@ func abs(x int) int {
151159
}
152160
```
153161

162+
#### TypeScript
163+
164+
```ts
165+
function largestTriangleArea(points: number[][]): number {
166+
let ans = 0;
167+
for (const [x1, y1] of points) {
168+
for (const [x2, y2] of points) {
169+
for (const [x3, y3] of points) {
170+
const u1 = x2 - x1,
171+
v1 = y2 - y1;
172+
const u2 = x3 - x1,
173+
v2 = y3 - y1;
174+
const t = Math.abs(u1 * v2 - u2 * v1) / 2;
175+
ans = Math.max(ans, t);
176+
}
177+
}
178+
}
179+
return ans;
180+
}
181+
```
182+
183+
#### Rust
184+
185+
```rust
186+
impl Solution {
187+
pub fn largest_triangle_area(points: Vec<Vec<i32>>) -> f64 {
188+
let mut ans: f64 = 0.0;
189+
for point1 in &points {
190+
let (x1, y1) = (point1[0], point1[1]);
191+
for point2 in &points {
192+
let (x2, y2) = (point2[0], point2[1]);
193+
for point3 in &points {
194+
let (x3, y3) = (point3[0], point3[1]);
195+
let u1 = x2 - x1;
196+
let v1 = y2 - y1;
197+
let u2 = x3 - x1;
198+
let v2 = y3 - y1;
199+
let t = ((u1 * v2 - u2 * v1) as f64).abs() / 2.0;
200+
ans = ans.max(t);
201+
}
202+
}
203+
}
204+
ans
205+
}
206+
}
207+
```
208+
154209
<!-- tabs:end -->
155210

156211
<!-- solution:end -->

‎solution/0800-0899/0812.Largest Triangle Area/README_EN.md‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ tags:
5151

5252
<!-- solution:start -->
5353

54-
### Solution 1
54+
### Solution: Enumerate Triangle Area Formula
55+
56+
Given three points $(x_1, y_1),ドル $(x_2, y_2),ドル $(x_3, y_3)$ on a plane, the area formula is:
57+
58+
$$S = \frac{1}{2} \left| x_1y_2 + x_2y_3 + x_3y_1 - x_1y_3 - x_2y_1 - x_3y_2 \right|$$
59+
60+
We can enumerate all combinations of three points and calculate the maximum area.
61+
62+
The time complexity is $O(n^3),ドル where $n$ is the number of points. The space complexity is $O(1)$.
5563

5664
<!-- tabs:start -->
5765

@@ -149,6 +157,53 @@ func abs(x int) int {
149157
}
150158
```
151159

160+
#### TypeScript
161+
162+
```ts
163+
function largestTriangleArea(points: number[][]): number {
164+
let ans = 0;
165+
for (const [x1, y1] of points) {
166+
for (const [x2, y2] of points) {
167+
for (const [x3, y3] of points) {
168+
const u1 = x2 - x1,
169+
v1 = y2 - y1;
170+
const u2 = x3 - x1,
171+
v2 = y3 - y1;
172+
const t = Math.abs(u1 * v2 - u2 * v1) / 2;
173+
ans = Math.max(ans, t);
174+
}
175+
}
176+
}
177+
return ans;
178+
}
179+
```
180+
181+
#### Rust
182+
183+
```rust
184+
impl Solution {
185+
pub fn largest_triangle_area(points: Vec<Vec<i32>>) -> f64 {
186+
let mut ans: f64 = 0.0;
187+
for point1 in &points {
188+
let (x1, y1) = (point1[0], point1[1]);
189+
for point2 in &points {
190+
let (x2, y2) = (point2[0], point2[1]);
191+
for point3 in &points {
192+
let (x3, y3) = (point3[0], point3[1]);
193+
let u1 = x2 - x1;
194+
let v1 = y2 - y1;
195+
let u2 = x3 - x1;
196+
let v2 = y3 - y1;
197+
let t = ((u1 * v2 - u2 * v1) as f64).abs() / 2.0;
198+
ans = ans.max(t);
199+
}
200+
}
201+
}
202+
ans
203+
}
204+
}
205+
```
206+
152207
<!-- tabs:end -->
153208

154209
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn largest_triangle_area(points: Vec<Vec<i32>>) -> f64 {
3+
let mut ans: f64 = 0.0;
4+
for point1 in &points {
5+
let (x1, y1) = (point1[0], point1[1]);
6+
for point2 in &points {
7+
let (x2, y2) = (point2[0], point2[1]);
8+
for point3 in &points {
9+
let (x3, y3) = (point3[0], point3[1]);
10+
let u1 = x2 - x1;
11+
let v1 = y2 - y1;
12+
let u2 = x3 - x1;
13+
let v2 = y3 - y1;
14+
let t = ((u1 * v2 - u2 * v1) as f64).abs() / 2.0;
15+
ans = ans.max(t);
16+
}
17+
}
18+
}
19+
ans
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function largestTriangleArea(points: number[][]): number {
2+
let ans = 0;
3+
for (const [x1, y1] of points) {
4+
for (const [x2, y2] of points) {
5+
for (const [x3, y3] of points) {
6+
const u1 = x2 - x1,
7+
v1 = y2 - y1;
8+
const u2 = x3 - x1,
9+
v2 = y3 - y1;
10+
const t = Math.abs(u1 * v2 - u2 * v1) / 2;
11+
ans = Math.max(ans, t);
12+
}
13+
}
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /