0
点赞
收藏
分享

微信扫一扫

LeetCode题解(0391):完美矩形(Python)


题目:​​原题链接​​(困难)

标签:扫描线算法、数学

解法

时间复杂度

空间复杂度

执行用时

Ans 1 (Python)

144ms (66.43%)

Ans 2 (Python)

Ans 3 (Python)

解法一:

class Solution:
def isRectangleCover(self, rectangles: List[List[int]]) -> bool:
count1 = collections.Counter()
mx1, my1, mx2, my2 = float("inf"), float("inf"), float("-inf"), float("-inf")
space = 0
for x1, y1, x2, y2 in rectangles:
count1[(x1, y1)] += 1
count1[(x1, y2)] += 1
count1[(x2, y1)] += 1
count1[(x2, y2)] += 1
space += (x2 - x1) * (y2 - y1)
mx1, my1, mx2, my2 = min(mx1, x1), min(my1, y1), max(mx2, x2), max(my2, y2)
count2 = collections.Counter(count1.values())
return (count2[1] == 4
and space == (mx2 - mx1) * (my2 - my1)
and len(rectangles) == len(set(tuple(elem) for elem in rectangles))
and count1[(mx1, my1)] == 1 and count1[(mx1, my2)] == 1
and count1[(mx2, my1)] == 1 and count1[(mx2, my2)] == 1)


举报

相关推荐

0 条评论