diff --git a/leetcode/901-1000/0939.Minimum-Area-Rectangle/1.jpeg b/leetcode/901-1000/0939.Minimum-Area-Rectangle/1.jpeg new file mode 100644 index 000000000..06b5ae908 Binary files /dev/null and b/leetcode/901-1000/0939.Minimum-Area-Rectangle/1.jpeg differ diff --git a/leetcode/901-1000/0939.Minimum-Area-Rectangle/2.jpeg b/leetcode/901-1000/0939.Minimum-Area-Rectangle/2.jpeg new file mode 100644 index 000000000..e55bbb64b Binary files /dev/null and b/leetcode/901-1000/0939.Minimum-Area-Rectangle/2.jpeg differ diff --git a/leetcode/901-1000/0939.Minimum-Area-Rectangle/README.md b/leetcode/901-1000/0939.Minimum-Area-Rectangle/README.md index b7a2b6a4f..f4bae7b08 100644 --- a/leetcode/901-1000/0939.Minimum-Area-Rectangle/README.md +++ b/leetcode/901-1000/0939.Minimum-Area-Rectangle/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution.go b/leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution.go index d115ccf5e..72c57fc51 100644 --- a/leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution.go +++ b/leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution.go @@ -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 } diff --git a/leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution_test.go b/leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution_test.go index 14ff50eb4..80f8ee8af 100644 --- a/leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution_test.go +++ b/leetcode/901-1000/0939.Minimum-Area-Rectangle/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }