feat: 812_largest_triangle_area

This commit is contained in:
SquidSpirit 2025-09-27 14:01:13 +08:00
parent 6a51947dcf
commit a908102fc1
3 changed files with 47 additions and 0 deletions

7
812_largest_triangle_area/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "largest_triangle_area"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "largest_triangle_area"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,34 @@
pub struct Solution;
impl Solution {
pub fn largest_triangle_area(points: Vec<Vec<i32>>) -> f64 {
let mut result = 0.0_f64;
for (a_index, a) in points.iter().enumerate() {
for (b_index, b) in points.iter().enumerate().skip(a_index + 1) {
for c in points.iter().skip(b_index + 1) {
let area = Solution::area((a[0], a[1]), (b[0], b[1]), (c[0], c[1]));
if area.is_nan() {
continue;
}
result = result.max(area);
}
}
}
result
}
fn distance(a: (i32, i32), b: (i32, i32)) -> f64 {
((a.0 - b.0).pow(2) as f64 + (a.1 - b.1).pow(2) as f64).sqrt()
}
fn area(a: (i32, i32), b: (i32, i32), c: (i32, i32)) -> f64 {
let ab = Solution::distance(a, b);
let bc = Solution::distance(b, c);
let ac = Solution::distance(a, c);
let s = (ab + bc + ac) / 2.0;
(s * (s - ab) * (s - bc) * (s - ac)).sqrt()
}
}