Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 939 #995

Merged
merged 1 commit into from
Oct 30, 2024
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 13 additions & 14 deletions leetcode/901-1000/0939.Minimum-Area-Rectangle/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [939.Minimum Area Rectangle][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
You are given an array of points in the **X-Y** plane `points` where `points[i] = [xi, yi]`.

Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is not any such rectangle, return `0`.

**Example 1:**

**Example 1:**
![1](./1.jpeg)

```
Input: a = "11", b = "1"
Output: "100"
Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.jpeg)

### 思路1
> ...
Minimum Area Rectangle
```go
```

Input: points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
```

## 结语

Expand Down
38 changes: 36 additions & 2 deletions leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(points [][]int) int {
keys := make(map[[2]int]struct{})
for _, xy := range points {
keys[[2]int{xy[0], xy[1]}] = struct{}{}
}

ans := 0
for i, xy := range points {
for j, other := range points {
if i == j {
continue
}
if xy[0] == other[0] || xy[1] == other[1] {
continue
}
x1, y1, x2, y2 := xy[0], other[1], other[0], xy[1]
_, ok1 := keys[[2]int{x1, y1}]
if ok1 {
if _, ok2 := keys[[2]int{x2, y2}]; ok2 {
width := x1 - x2
if width < 0 {
width = -width
}
height := y1 - y2
if height < 0 {
height = -height
}
area := width * height
if ans == 0 || ans > area {
ans = area
}
}
}
}
}
return ans
}
13 changes: 6 additions & 7 deletions leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 1}, {1, 3}, {3, 1}, {3, 3}, {2, 2}}, 4},
{"TestCase2", [][]int{{1, 1}, {1, 3}, {3, 1}, {3, 3}, {4, 1}, {4, 3}}, 2},
}

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

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

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