0
点赞
收藏
分享

微信扫一扫

LeetCode题解(0850):矩形面积II(Python)


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

标签:堆、线段树

解法

时间复杂度

空间复杂度

执行用时

Ans 1 (Python)

64ms (81.58%)

Ans 2 (Python)

Ans 3 (Python)

解法一:

import heapq
from typing import List


def count_num(lst):
"""根据[[l1,r1],[l2,r2]]线段列表,计算线段总长度(重合部分只记一次)"""
if not lst:
return 0

lst.sort()

ans = 0
s, e = lst[0]
for i in range(1, len(lst)):
l, r = lst[i]
if l > e:
ans += e - s
s, e = l, r
else:
e = max(e, r)
ans += (e - s)
return ans


class Solution:
_MOD = 10 ** 9 + 7

def rectangleArea(self, rectangles: List[List[int]]) -> int:
rectangles.sort(key=lambda x: (x[0], x[2]))
# print(rectangles)

# 坐标压缩
coords = set()
for x1, y1, x2, y2 in rectangles:
coords.add(x1)
coords.add(x2)
coords = sorted(coords)

i = 0 # 当前长方形列表的遍历位置
now = [] # 当前位置所有长方形右边、下边、上边

ans = 0
for j in range(len(coords) - 1):
d = coords[j + 1] - coords[j]

while i < len(rectangles) and rectangles[i][0] == coords[j]:
heapq.heappush(now, (rectangles[i][2], rectangles[i][1], rectangles[i][3]))
i += 1

while now and now[0][0] == coords[j]:
heapq.heappop(now)

print(j, coords[j], ":", now, "*", d, "=", count_num([[y1, y2] for x2, y1, y2 in now]), "*", d)

ans += count_num([[y1, y2] for x2, y1, y2 in now]) * d

return ans % self._MOD


举报

相关推荐

0 条评论