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
Binary file added leetcode/801-900/0812.Largest-Triangle-Area/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 11 additions & 15 deletions leetcode/801-900/0812.Largest-Triangle-Area/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
32 changes: 30 additions & 2 deletions leetcode/801-900/0812.Largest-Triangle-Area/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 9 additions & 7 deletions leetcode/801-900/0812.Largest-Triangle-Area/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +32,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading