836.询问两个矩形是否overlap,223.求两个矩形面积之和,都是先判断是否overlap就行
1 #836
2 #Runtime: 39 ms
3 #Memory Usage: 13.3 MB
4
5 class Solution(object):
6 def isRectangleOverlap(self, rec1, rec2):
7 """
8 :type rec1: List[int]
9 :type rec2: List[int]
10 :rtype: bool
11 """
12 return max(rec1[0], rec2[0]) < min(rec1[2], rec2[2]) and max(rec1[1], rec2[1]) < min(rec1[3], rec2[3])
1 #223
2 #Runtime: 74 ms
3 #Memory Usage: 13.5 MB
4
5 class Solution(object):
6 def computeArea(self, ax1, ay1, ax2, ay2, bx1, by1, bx2, by2):
7 """
8 :type ax1: int
9 :type ay1: int
10 :type ax2: int
11 :type ay2: int
12 :type bx1: int
13 :type by1: int
14 :type bx2: int
15 :type by2: int
16 :rtype: int
17 """
18 max_x1 = max(ax1, bx1)
19 min_x2 = min(ax2, bx2)
20 max_y1 = max(ay1, by1)
21 min_y2 = min(ay2, by2)
22 overlap_area = 0
23 if max_x1 < min_x2 and max_y1 < min_y2:
24 overlap_area = (min_x2 - max_x1) * (min_y2 - max_y1)
25 return (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - overlap_area