-
Notifications
You must be signed in to change notification settings - Fork 0
/
187.py
94 lines (74 loc) · 2.57 KB
/
187.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Problem:
You are given given a list of rectangles represented by min and max x- and
y-coordinates. Compute whether or not a pair of rectangles overlap each other. If one
rectangle completely covers another, it is considered overlapping.
For example, given the following rectangles:
{
"top_left": (1, 4),
"dimensions": (3, 3) # width, height
},
{
"top_left": (-1, 3),
"dimensions": (2, 1)
},
{
"top_left": (0, 5),
"dimensions": (4, 3)
}
return true as the first and third rectangle overlap each other.
"""
from typing import Dict, List, Tuple
Rectangle = Dict[str, Tuple[int, int]]
def get_intersection_area(rect1: List[Rectangle], rect2: List[Rectangle]) -> int:
if rect1["top_left"][0] < rect2["top_left"][0]:
left = rect1
right = rect2
else:
left = rect2
right = rect1
if rect1["top_left"][1] > rect2["top_left"][1]:
top = rect1
bottom = rect2
else:
top = rect2
bottom = rect1
if (left["top_left"][0] + left["dimensions"][0]) < right["top_left"][0]:
return 0
else:
span_x = (left["top_left"][0] + left["dimensions"][0]) - right["top_left"][0]
if (top["top_left"][1] - top["dimensions"][1]) > bottom["top_left"][1]:
return 0
else:
span_y = bottom["top_left"][1] - (top["top_left"][1] - top["dimensions"][1])
return span_x * span_y
def get_covered_area(rect: Rectangle) -> int:
width, height = rect["dimensions"]
return width * height
def check_rectangles_intersection(rectangles: List[Rectangle]) -> bool:
length = len(rectangles)
# checking for intersection for each pair of rectangles
for i in range(length - 1):
for j in range(i + 1, length):
intersection_area = get_intersection_area(rectangles[i], rectangles[j])
rect1_area = get_covered_area(rectangles[i])
rect2_area = get_covered_area(rectangles[j])
if intersection_area in (rect1_area, rect2_area):
return True
return False
if __name__ == "__main__":
# NOTE: THE QUESTION STATEMENT IS WRONG THE RECTANGLES 1 & 3 DOES NOT OVERLAP BUT
# ONLY INTERSECT (SMALL MODIFICATION DONE TO MAKE THEM OVERLAP)
rectangles = [
{"top_left": (1, 4), "dimensions": (3, 3)},
{"top_left": (-1, 3), "dimensions": (2, 1)},
{"top_left": (0, 5), "dimensions": (4, 4)}, # MODIFICATION
]
print(check_rectangles_intersection(rectangles))
rectangles.pop()
print(check_rectangles_intersection(rectangles))
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(1)
"""