Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion solution/0800-0899/0812.Largest Triangle Area/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:枚举三点面积公式

给定平面上三点 $(x_1, y_1)$, $(x_2, y_2)$, $(x_3, y_3)$,其面积公式为:

$$S = \frac{1}{2} \left| x_1y_2 + x_2y_3 + x_3y_1 - x_1y_3 - x_2y_1 - x_3y_2 \right|$$

我们可以枚举所有的三点组合,计算面积的最大值。

时间复杂度 $O(n^3)$,其中 $n$ 是点的数量。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -151,6 +159,53 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function largestTriangleArea(points: number[][]): number {
let ans = 0;
for (const [x1, y1] of points) {
for (const [x2, y2] of points) {
for (const [x3, y3] of points) {
const u1 = x2 - x1,
v1 = y2 - y1;
const u2 = x3 - x1,
v2 = y3 - y1;
const t = Math.abs(u1 * v2 - u2 * v1) / 2;
ans = Math.max(ans, t);
}
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn largest_triangle_area(points: Vec<Vec<i32>>) -> f64 {
let mut ans: f64 = 0.0;
for point1 in &points {
let (x1, y1) = (point1[0], point1[1]);
for point2 in &points {
let (x2, y2) = (point2[0], point2[1]);
for point3 in &points {
let (x3, y3) = (point3[0], point3[1]);
let u1 = x2 - x1;
let v1 = y2 - y1;
let u2 = x3 - x1;
let v2 = y3 - y1;
let t = ((u1 * v2 - u2 * v1) as f64).abs() / 2.0;
ans = ans.max(t);
}
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
57 changes: 56 additions & 1 deletion solution/0800-0899/0812.Largest Triangle Area/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution: Enumerate Triangle Area Formula

Given three points $(x_1, y_1)$, $(x_2, y_2)$, $(x_3, y_3)$ on a plane, the area formula is:

$$S = \frac{1}{2} \left| x_1y_2 + x_2y_3 + x_3y_1 - x_1y_3 - x_2y_1 - x_3y_2 \right|$$

We can enumerate all combinations of three points and calculate the maximum area.

The time complexity is $O(n^3)$, where $n$ is the number of points. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -149,6 +157,53 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function largestTriangleArea(points: number[][]): number {
let ans = 0;
for (const [x1, y1] of points) {
for (const [x2, y2] of points) {
for (const [x3, y3] of points) {
const u1 = x2 - x1,
v1 = y2 - y1;
const u2 = x3 - x1,
v2 = y3 - y1;
const t = Math.abs(u1 * v2 - u2 * v1) / 2;
ans = Math.max(ans, t);
}
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn largest_triangle_area(points: Vec<Vec<i32>>) -> f64 {
let mut ans: f64 = 0.0;
for point1 in &points {
let (x1, y1) = (point1[0], point1[1]);
for point2 in &points {
let (x2, y2) = (point2[0], point2[1]);
for point3 in &points {
let (x3, y3) = (point3[0], point3[1]);
let u1 = x2 - x1;
let v1 = y2 - y1;
let u2 = x3 - x1;
let v2 = y3 - y1;
let t = ((u1 * v2 - u2 * v1) as f64).abs() / 2.0;
ans = ans.max(t);
}
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
21 changes: 21 additions & 0 deletions solution/0800-0899/0812.Largest Triangle Area/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
impl Solution {
pub fn largest_triangle_area(points: Vec<Vec<i32>>) -> f64 {
let mut ans: f64 = 0.0;
for point1 in &points {
let (x1, y1) = (point1[0], point1[1]);
for point2 in &points {
let (x2, y2) = (point2[0], point2[1]);
for point3 in &points {
let (x3, y3) = (point3[0], point3[1]);
let u1 = x2 - x1;
let v1 = y2 - y1;
let u2 = x3 - x1;
let v2 = y3 - y1;
let t = ((u1 * v2 - u2 * v1) as f64).abs() / 2.0;
ans = ans.max(t);
}
}
}
ans
}
}
16 changes: 16 additions & 0 deletions solution/0800-0899/0812.Largest Triangle Area/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function largestTriangleArea(points: number[][]): number {
let ans = 0;
for (const [x1, y1] of points) {
for (const [x2, y2] of points) {
for (const [x3, y3] of points) {
const u1 = x2 - x1,
v1 = y2 - y1;
const u2 = x3 - x1,
v2 = y3 - y1;
const t = Math.abs(u1 * v2 - u2 * v1) / 2;
ans = Math.max(ans, t);
}
}
}
return ans;
}