Leetcode 836 Rectangle Overlap

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner.

Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap.

Given two rectangles, return whether they overlap.

Example 1:
Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true

Example 2:
Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false

Notes:
1.Both rectangles rec1 and rec2 are lists of 4 integers.
2.All coordinates in rectangles will be between -10^9 and 10^9.

分析:
常见的矩阵题型,由左下和右上两个点确定一个矩形,判断两个矩形是否有重叠部分,这和之前有一道题非常的相似,那道题是直接求重叠面积

思路:
记住下面这个固定公式,很有用

1
2
3
4
5
6
7
8
9
10
11
12
13
def isRectangleOverlap(rec1, rec2):
"""
:type rec1: List[int]
:type rec2: List[int]
:rtype: bool
"""
a,b,c,d = rec1
e,f,g,h = rec2
return max(0,min(c,g)-max(e,a)) * max(0,min(d,h)-max(f,b)) != 0

43 / 43 test cases passed.
difficulty: easy
Runtime: 31 ms