Skip to content

Commit fd41bd9

Browse files
authored
Merge pull request halfrost#191 from gostool/leetcode0391
Leetcode0391
2 parents 1bce2a9 + 6df3396 commit fd41bd9

File tree

3 files changed

+221
-0
lines changed

3 files changed

+221
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
type point struct {
4+
x int
5+
y int
6+
}
7+
8+
func isRectangleCover(rectangles [][]int) bool {
9+
minX, minY, maxA, maxB := rectangles[0][0], rectangles[0][1], rectangles[0][2], rectangles[0][3]
10+
area := 0
11+
cnt := make(map[point]int)
12+
for _, v := range rectangles {
13+
x, y, a, b := v[0], v[1], v[2], v[3]
14+
area += (a - x) * (b - y)
15+
minX = min(minX, x)
16+
minY = min(minY, y)
17+
maxA = max(maxA, a)
18+
maxB = max(maxB, b)
19+
cnt[point{x, y}]++
20+
cnt[point{a, b}]++
21+
cnt[point{x, b}]++
22+
cnt[point{a, y}]++
23+
}
24+
if area != (maxA-minX)*(maxB-minY) ||
25+
cnt[point{minX, minY}] != 1 || cnt[point{maxA, maxB}] != 1 ||
26+
cnt[point{minX, maxB}] != 1 || cnt[point{maxA, minY}] != 1 {
27+
return false
28+
}
29+
delete(cnt, point{minX, minY})
30+
delete(cnt, point{maxA, maxB})
31+
delete(cnt, point{minX, maxB})
32+
delete(cnt, point{maxA, minY})
33+
for _, v := range cnt {
34+
if v != 2 && v != 4 {
35+
return false
36+
}
37+
}
38+
return true
39+
}
40+
41+
func min(a, b int) int {
42+
if a < b {
43+
return a
44+
}
45+
return b
46+
}
47+
48+
func max(a, b int) int {
49+
if a > b {
50+
return a
51+
}
52+
return b
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question391 struct {
9+
para391
10+
ans391
11+
}
12+
13+
// para 是参数
14+
type para391 struct {
15+
rectangles [][]int
16+
}
17+
18+
// ans 是答案
19+
type ans391 struct {
20+
ans bool
21+
}
22+
23+
func Test_Problem391(t *testing.T) {
24+
25+
qs := []question391{
26+
27+
{
28+
para391{[][]int{{1, 1, 3, 3}, {3, 1, 4, 2}, {3, 2, 4, 4}, {1, 3, 2, 4}, {2, 3, 3, 4}}},
29+
ans391{true},
30+
},
31+
32+
{
33+
para391{[][]int{{1, 1, 2, 3}, {1, 3, 2, 4}, {3, 1, 4, 2}, {3, 2, 4, 4}}},
34+
ans391{false},
35+
},
36+
37+
{
38+
para391{[][]int{{1, 1, 3, 3}, {3, 1, 4, 2}, {1, 3, 2, 4}, {3, 2, 4, 4}}},
39+
ans391{false},
40+
},
41+
42+
{
43+
para391{[][]int{{1, 1, 3, 3}, {3, 1, 4, 2}, {1, 3, 2, 4}, {2, 2, 4, 4}}},
44+
ans391{false},
45+
},
46+
}
47+
48+
fmt.Printf("------------------------Leetcode Problem 391------------------------\n")
49+
50+
for _, q := range qs {
51+
_, p := q.ans391, q.para391
52+
fmt.Printf("【input】:%v 【output】:%v\n", p, isRectangleCover(p.rectangles))
53+
}
54+
fmt.Printf("\n\n\n")
55+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# [391. Perfect Rectangle](https://leetcode-cn.com/problems/perfect-rectangle/)
2+
3+
## 题目
4+
5+
Given an array rectangles where rectangles[i] = [xi, yi, ai, bi] represents an axis-aligned rectangle. The bottom-left point of the rectangle is (xi, yi) and the top-right point of it is (ai, bi).
6+
7+
Return true if all the rectangles together form an exact cover of a rectangular region.
8+
9+
**Example1:**
10+
11+
![https://assets.leetcode.com/uploads/2021/03/27/perectrec1-plane.jpg](https://assets.leetcode.com/uploads/2021/03/27/perectrec1-plane.jpg)
12+
13+
Input: rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
14+
Output: true
15+
Explanation: All 5 rectangles together form an exact cover of a rectangular region.
16+
17+
**Example2:**
18+
19+
![https://assets.leetcode.com/uploads/2021/03/27/perfectrec2-plane.jpg](https://assets.leetcode.com/uploads/2021/03/27/perfectrec2-plane.jpg)
20+
21+
Input: rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]
22+
Output: false
23+
Explanation: Because there is a gap between the two rectangular regions.
24+
25+
**Example3:**
26+
27+
![https://assets.leetcode.com/uploads/2021/03/27/perfectrec3-plane.jpg](https://assets.leetcode.com/uploads/2021/03/27/perfectrec3-plane.jpg)
28+
29+
Input: rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[3,2,4,4]]
30+
Output: false
31+
Explanation: Because there is a gap in the top center.
32+
33+
**Example4:**
34+
35+
![https://assets.leetcode.com/uploads/2021/03/27/perfecrrec4-plane.jpg](https://assets.leetcode.com/uploads/2021/03/27/perfecrrec4-plane.jpg)
36+
37+
Input: rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]]
38+
Output: false
39+
Explanation: Because two of the rectangles overlap with each other.
40+
41+
**Constraints:**
42+
43+
- 1 <= rectangles.length <= 2 * 10000
44+
- rectangles[i].length == 4
45+
- -100000 <= xi, yi, ai, bi <= 100000
46+
47+
## 题目大意
48+
49+
给你一个数组 rectangles ,其中 rectangles[i] = [xi, yi, ai, bi] 表示一个坐标轴平行的矩形。这个矩形的左下顶点是 (xi, yi) ,右上顶点是 (ai, bi) 。
50+
51+
如果所有矩形一起精确覆盖了某个矩形区域,则返回 true ;否则,返回 false 。
52+
53+
## 解题思路
54+
55+
- 矩形区域的面积等于所有矩形的面积之和并且满足矩形区域四角的顶点只能出现一次,且其余顶点的出现次数只能是两次或四次则返回true,否则返回false
56+
57+
## 代码
58+
59+
```go
60+
package leetcode
61+
62+
type point struct {
63+
x int
64+
y int
65+
}
66+
67+
func isRectangleCover(rectangles [][]int) bool {
68+
minX, minY, maxA, maxB := rectangles[0][0], rectangles[0][1], rectangles[0][2], rectangles[0][3]
69+
area := 0
70+
cnt := make(map[point]int)
71+
for _, v := range rectangles {
72+
x, y, a, b := v[0], v[1], v[2], v[3]
73+
area += (a - x) * (b - y)
74+
minX = min(minX, x)
75+
minY = min(minY, y)
76+
maxA = max(maxA, a)
77+
maxB = max(maxB, b)
78+
cnt[point{x, y}]++
79+
cnt[point{a, b}]++
80+
cnt[point{x, b}]++
81+
cnt[point{a, y}]++
82+
}
83+
if area != (maxA - minX) * (maxB - minY) ||
84+
cnt[point{minX, minY}] != 1 || cnt[point{maxA, maxB}] != 1 ||
85+
cnt[point{minX, maxB}] != 1 || cnt[point{maxA, minY}] != 1 {
86+
return false
87+
}
88+
delete(cnt, point{minX, minY})
89+
delete(cnt, point{maxA, maxB})
90+
delete(cnt, point{minX, maxB})
91+
delete(cnt, point{maxA, minY})
92+
for _, v := range cnt {
93+
if v != 2 && v != 4 {
94+
return false
95+
}
96+
}
97+
return true
98+
}
99+
100+
func min(a, b int) int {
101+
if a < b {
102+
return a
103+
}
104+
return b
105+
}
106+
107+
func max(a, b int) int {
108+
if a > b {
109+
return a
110+
}
111+
return b
112+
}
113+
```

0 commit comments

Comments
 (0)