Skip to content

Commit

Permalink
Add solution and test-cases for problem 939
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Sep 29, 2024
1 parent ffaca5d commit f8b377f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 23 deletions.
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() {
}

0 comments on commit f8b377f

Please sign in to comment.