Skip to content

Commit 644fcdb

Browse files
committed
Create rectangle-overlap.py
1 parent 7c0e7ff commit 644fcdb

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Python/rectangle-overlap.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Time: O(1)
2+
# Space: O(1)
3+
4+
# A rectangle is represented as a list [x1, y1, x2, y2],
5+
# where (x1, y1) are the coordinates of its bottom-left corner,
6+
# and (x2, y2) are the coordinates of its top-right corner.
7+
#
8+
# Two rectangles overlap if
9+
# the area of their intersection is positive.
10+
# To be clear, two rectangles that only
11+
# touch at the corner or edges do not overlap.
12+
#
13+
# Given two rectangles, return whether they overlap.
14+
#
15+
# Example 1:
16+
#
17+
# Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
18+
# Output: true
19+
# Example 2:
20+
#
21+
# Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
22+
# Output: false
23+
# Notes:
24+
#
25+
# Both rectangles rec1 and rec2 are lists of 4 integers.
26+
# All coordinates in rectangles will be between -10^9 and 10^9.
27+
28+
29+
class Solution(object):
30+
def isRectangleOverlap(self, rec1, rec2):
31+
"""
32+
:type rec1: List[int]
33+
:type rec2: List[int]
34+
:rtype: bool
35+
"""
36+
def intersect(p_left, p_right, q_left, q_right):
37+
return max(p_left, q_left) < min(p_right, q_right)
38+
39+
return (intersect(rec1[0], rec1[2], rec2[0], rec2[2]) and
40+
intersect(rec1[1], rec1[3], rec2[1], rec2[3]))

0 commit comments

Comments
 (0)