Skip to content

Commit 62eddfb

Browse files
authored
Merge pull request #1324 from 0xff-dev/812
Add solution and test-cases for problem 812
2 parents 7b54dbd + f44d4c1 commit 62eddfb

File tree

4 files changed

+50
-24
lines changed

4 files changed

+50
-24
lines changed
7.31 KB
Loading

leetcode/801-900/0812.Largest-Triangle-Area/README.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
# [812.Largest Triangle Area][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
**Example 1:**
77

8-
**Example 1:**
8+
![1](./1.png)
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
12+
Output: 2.00000
13+
Explanation: The five points are shown in the above figure. The red triangle is the largest.
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**Example 2:**
1917

20-
### 思路1
21-
> ...
22-
Largest Triangle Area
23-
```go
2418
```
25-
19+
Input: points = [[1,0],[0,0],[0,1]]
20+
Output: 0.50000
21+
```
2622

2723
## 结语
2824

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
const epsilon = 1e-9
6+
7+
func canForm(a, b, c []int) (float64, bool) {
8+
abx := b[0] - a[0]
9+
aby := b[1] - a[1]
10+
acx := c[0] - a[0]
11+
acy := c[1] - a[1]
12+
area := math.Abs(float64(abx*acy - aby*acx))
13+
if area > epsilon {
14+
return area, true
15+
}
16+
return 0.0, false
17+
}
18+
19+
func Solution(points [][]int) float64 {
20+
var ret float64
21+
l := len(points)
22+
for i := 0; i < l-2; i++ {
23+
for j := i + 1; j < l-1; j++ {
24+
for k := j + 1; k < l; k++ {
25+
area, ok := canForm(points[i], points[j], points[k])
26+
if ok {
27+
ret = max(ret, area/2.0)
28+
}
29+
}
30+
}
31+
}
32+
return ret
533
}

leetcode/801-900/0812.Largest-Triangle-Area/Solution_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect float64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{
17+
{0, 0}, {0, 1}, {1, 0}, {0, 2}, {2, 0}}, 2.00000},
18+
{"TestCase", [][]int{
19+
{1, 0}, {0, 0}, {0, 1},
20+
}, 0.50000},
1921
}
2022

2123
// 开始测试
@@ -30,10 +32,10 @@ func TestSolution(t *testing.T) {
3032
}
3133
}
3234

33-
// 压力测试
35+
// 压力测试
3436
func BenchmarkSolution(b *testing.B) {
3537
}
3638

37-
// 使用案列
39+
// 使用案列
3840
func ExampleSolution() {
3941
}

0 commit comments

Comments
 (0)