Skip to content

Commit 39f57dd

Browse files
authored
Create circle-and-rectangle-overlapping.py
1 parent 92ed5c9 commit 39f57dd

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Time: O(1)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def checkOverlap(self, radius, x_center, y_center, x1, y1, x2, y2):
6+
"""
7+
:type radius: int
8+
:type x_center: int
9+
:type y_center: int
10+
:type x1: int
11+
:type y1: int
12+
:type x2: int
13+
:type y2: int
14+
:rtype: bool
15+
"""
16+
x1 -= x_center
17+
y1 -= y_center
18+
x2 -= x_center
19+
y2 -= y_center
20+
x = x1 if x1 > 0 else x2 if x2 < 0 else 0
21+
y = y1 if y1 > 0 else y2 if y2 < 0 else 0
22+
return x**2 + y**2 <= radius**2
23+
24+
25+
# Time: O(1)
26+
# Space: O(1)
27+
class Solution2(object):
28+
def checkOverlap(self, radius, x_center, y_center, x1, y1, x2, y2):
29+
"""
30+
:type radius: int
31+
:type x_center: int
32+
:type y_center: int
33+
:type x1: int
34+
:type y1: int
35+
:type x2: int
36+
:type y2: int
37+
:rtype: bool
38+
"""
39+
x1 -= x_center
40+
y1 -= y_center
41+
x2 -= x_center
42+
y2 -= y_center
43+
x = min(abs(x1), abs(x2)) if x1*x2 > 0 else 0
44+
y = min(abs(y1), abs(y2)) if y1*y2 > 0 else 0
45+
return x**2 + y**2 <= radius**2

0 commit comments

Comments
 (0)