1
class Solution:
2
def isRectangleCover(self, rectangles: List[List[int]]) -> bool:
3
X1, Y1 = float("inf"), float("inf")
4
X2, Y2 = -float("inf"), -float("inf")
5

6
points = set()
7
actual_area = 0
8
for x1, y1, x2, y2 in rectangles:
9
# calculate the coords of the potential perfect rectangle
10
X1, Y1 = min(X1, x1), min(Y1, y1)
11
X2, Y2 = max(X2, x2), max(Y2, y2)
12
# add up to the actual_area, so we can check against to see if thers is any part overwritten.
13
actual_area += (x2 - x1) * (y2 - y1)
14

15
# proving steps in https://labuladong.github.io/algo/4/32/131/
16
for p in [(x1, y1), (x1, y2), (x2, y1), (x2, y2)]:
17
if p in points:
18
points.remove(p)
19
else:
20
points.add(p)
21

22
# check the area
23
expected_area = (X2 - X1) * (Y2 - Y1)
24
if actual_area != expected_area:
25
return False
26

27
if len(points) != 4:
28
return False
29

30
if (X1, Y1) not in points:
31
return False
32
if (X1, Y2) not in points:
33
return False
34
if (X2, Y1) not in points:
35
return False
36
if (X2, Y2) not in points:
37
return False
38

39
return True

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0