diff --git a/leetcode/801-900/0812.Largest-Triangle-Area/1.png b/leetcode/801-900/0812.Largest-Triangle-Area/1.png new file mode 100644 index 000000000..611d67dd9 Binary files /dev/null and b/leetcode/801-900/0812.Largest-Triangle-Area/1.png differ diff --git a/leetcode/801-900/0812.Largest-Triangle-Area/README.md b/leetcode/801-900/0812.Largest-Triangle-Area/README.md index 133579c68..631565ef8 100644 --- a/leetcode/801-900/0812.Largest-Triangle-Area/README.md +++ b/leetcode/801-900/0812.Largest-Triangle-Area/README.md @@ -1,28 +1,24 @@ # [812.Largest Triangle Area][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return the area of the largest triangle that can be formed by any three different points. Answers within 10-5 of the actual answer will be accepted. + +**Example 1:** -**Example 1:** +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] +Output: 2.00000 +Explanation: The five points are shown in the above figure. The red triangle is the largest. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Largest Triangle Area -```go ``` - +Input: points = [[1,0],[0,0],[0,1]] +Output: 0.50000 +``` ## 结语 diff --git a/leetcode/801-900/0812.Largest-Triangle-Area/Solution.go b/leetcode/801-900/0812.Largest-Triangle-Area/Solution.go index d115ccf5e..9e418367f 100644 --- a/leetcode/801-900/0812.Largest-Triangle-Area/Solution.go +++ b/leetcode/801-900/0812.Largest-Triangle-Area/Solution.go @@ -1,5 +1,33 @@ package Solution -func Solution(x bool) bool { - return x +import "math" + +const epsilon = 1e-9 + +func canForm(a, b, c []int) (float64, bool) { + abx := b[0] - a[0] + aby := b[1] - a[1] + acx := c[0] - a[0] + acy := c[1] - a[1] + area := math.Abs(float64(abx*acy - aby*acx)) + if area > epsilon { + return area, true + } + return 0.0, false +} + +func Solution(points [][]int) float64 { + var ret float64 + l := len(points) + for i := 0; i < l-2; i++ { + for j := i + 1; j < l-1; j++ { + for k := j + 1; k < l; k++ { + area, ok := canForm(points[i], points[j], points[k]) + if ok { + ret = max(ret, area/2.0) + } + } + } + } + return ret } diff --git a/leetcode/801-900/0812.Largest-Triangle-Area/Solution_test.go b/leetcode/801-900/0812.Largest-Triangle-Area/Solution_test.go index 14ff50eb4..9b4619d5b 100644 --- a/leetcode/801-900/0812.Largest-Triangle-Area/Solution_test.go +++ b/leetcode/801-900/0812.Largest-Triangle-Area/Solution_test.go @@ -10,12 +10,14 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]int + expect float64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{ + {0, 0}, {0, 1}, {1, 0}, {0, 2}, {2, 0}}, 2.00000}, + {"TestCase", [][]int{ + {1, 0}, {0, 0}, {0, 1}, + }, 0.50000}, } // 开始测试 @@ -30,10 +32,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }