美文网首页leetcode
850. Rectangle Area II

850. Rectangle Area II

作者: 十月里的男艺术家 | 来源:发表于2020-03-01 12:23 被阅读0次

题目:

850. Rectangle Area II

We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle.

Find the total area covered by all rectangles in the plane. Since the answer may be too large, return it modulo 10^9 + 7.

Example 1:

Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
Output: 6
Explanation: As illustrated in the picture.

Example 2:

Input: [[0,0,1000000000,1000000000]]
Output: 49
Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49.

Note:

  • 1 <= rectangles.length <= 200
  • rectanges[i].length = 4
  • 0 <= rectangles[i][j] <= 10^9
  • The total area covered by all rectangles will never exceed 2^63 - 1 and thus will fit in a 64-bit signed integer.

850. 矩形面积 II

我们给出了一个(轴对齐的)矩形列表 rectangles 。 对于 rectangle[i] = [x1, y1, x2, y2],其中(x1,y1)是矩形 i 左下角的坐标,(x2,y2)是该矩形右上角的坐标。

找出平面中所有矩形叠加覆盖后的总面积。 由于答案可能太大,请返回它对 10 ^ 9 + 7 取模的结果。

示例 1:

输入:[[0,0,2,2],[1,0,2,3],[1,0,3,1]]
输出:6
解释:如图所示。

示例 2:

输入:[[0,0,1000000000,1000000000]]
输出:49
解释:答案是 10^18 对 (10^9 + 7) 取模的结果, 即 (10^9)^2 → (-7)^2 = 49 。

提示:

  • 1 <= rectangles.length <= 200
  • rectanges[i].length = 4
  • 0 <= rectangles[i][j] <= 10^9
  • 矩形叠加覆盖后的总面积不会超越 2^63 - 1 ,这意味着可以用一个 64 位有符号整数来保存面积结果。

思路:

按照x轴扫描,计算相应x范围内高度,计算面积求和

按照y轴扫描,计算相应y范围内宽度,计算面积求和

代码:

class Solution:
    def rectangleArea(self, rectangles: List[List[int]]) -> int:
        area = 0
        rects = [item for x1, y1, x2, y2 in rectangles for item in [[x1, y1, y2, 0], [x2, y1, y2, 1]]]
        last = rects[0][0]
        heap = [(float('inf'), float('inf'))]
        for x, y1, y2, k in sorted(rects):
            h = start = end = 0
            for m, n in heap:
                if m > end:
                    h += end - start
                    start, end = m, n
                else:
                    end = max(end, n)
            area += (x-last)*h
            import bisect
            heap.remove((y1, y2)) if k else bisect.insort(heap, (y1, y2))
            last = x

        return area % (10**9 + 7)

    def rectangleAreaV01(self, rectangles: List[List[int]]) -> int:

        xs = sorted(set([x for x1, y1, x2, y2 in rectangles for x in [x1, x2]]))
        count = [0]*len(xs)
        xi = {v: i for i, v in enumerate(xs)}
        L = []
        for x1, y1, x2, y2 in rectangles:
            L.append((y1, x1, x2, 1))
            L.append((y2, x1, x2, -1))
        L.sort()

        cur_sum = 0
        area = 0
        cur_y = 0

        for y, x1, x2, sig in L:
            area += (y-cur_y)*cur_sum
            cur_y = y
            for i in range(xi[x1], xi[x2]):
                count[i] += sig
            cur_sum = sum(x2-x1 if c else 0 for x1, x2, c in zip(xs, xs[1:], count))
        return area % (10 ** 9 + 7)

相关文章

网友评论

    本文标题:850. Rectangle Area II

    本文链接:https://www.haomeiwen.com/subject/kpdfkhtx.html