diff --git a/solution/0800-0899/0812.Largest Triangle Area/README.md b/solution/0800-0899/0812.Largest Triangle Area/README.md index f7c8d86fcc96e..507a3db440afc 100644 --- a/solution/0800-0899/0812.Largest Triangle Area/README.md +++ b/solution/0800-0899/0812.Largest Triangle Area/README.md @@ -53,7 +53,15 @@ tags: -### 方法一 +### 方法一:枚举三点面积公式 + +给定平面上三点 $(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)$。 @@ -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>) -> 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 + } +} +``` + diff --git a/solution/0800-0899/0812.Largest Triangle Area/README_EN.md b/solution/0800-0899/0812.Largest Triangle Area/README_EN.md index e91f788e5fca4..3b70c62ae3a94 100644 --- a/solution/0800-0899/0812.Largest Triangle Area/README_EN.md +++ b/solution/0800-0899/0812.Largest Triangle Area/README_EN.md @@ -51,7 +51,15 @@ tags: -### 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)$. @@ -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>) -> 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 + } +} +``` + diff --git a/solution/0800-0899/0812.Largest Triangle Area/Solution.rs b/solution/0800-0899/0812.Largest Triangle Area/Solution.rs new file mode 100644 index 0000000000000..64c8bcac5b66a --- /dev/null +++ b/solution/0800-0899/0812.Largest Triangle Area/Solution.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn largest_triangle_area(points: Vec>) -> 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 + } +} diff --git a/solution/0800-0899/0812.Largest Triangle Area/Solution.ts b/solution/0800-0899/0812.Largest Triangle Area/Solution.ts new file mode 100644 index 0000000000000..fee1a280741bb --- /dev/null +++ b/solution/0800-0899/0812.Largest Triangle Area/Solution.ts @@ -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; +}