391. 完美矩形
为保证权益,题目请参考 391. 完美矩形(From LeetCode).
解决方案1
Python
python
# 391. 完美矩形
# https://leetcode-cn.com/problems/perfect-rectangle/
################################################################################
from typing import List
class Solution:
def isRectangleCover(self, rectangles: List[List[int]]) -> bool:
tris = {}
def add(x, y, tris):
if x in tris:
if y in tris[x]:
tris[x][y] += 1
else:
tris[x][y] = 1
else:
tris[x] = {y: 1}
area = 0
minx = 10000000000
miny = 10000000000
maxa = -10000000000
maxb = -10000000000
for x, y, a, b in rectangles:
add(x, y, tris)
add(a, y, tris)
add(a, b, tris)
add(x, b, tris)
area += (b - y) * (a - x)
minx = min(minx, x)
miny = min(miny, y)
maxa = max(maxa, a)
maxb = max(maxb, b)
if (
area != (maxa - minx) * (maxb - miny)
or (miny in tris[minx] and tris[minx][miny] != 1)
or (maxb in tris[minx] and tris[minx][maxb] != 1)
or (miny in tris[maxa] and tris[maxa][miny] != 1)
or (maxb in tris[maxa] and tris[maxa][maxb] != 1)
):
return False
cou = 0
for k, v in tris.items():
for k2, v2 in v.items():
if v2 == 2 or v2 == 4:
pass
elif v2 == 1:
cou += 1
else:
return False
return cou == 4
################################################################################
if __name__ == "__main__":
solution = Solution()
print(solution.isRectangleCover([[1,1,2,2],[0,1,1,2],[1,0,2,1],[0,2,3,3],[2,0,3,3]]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66