Leetcode: Rectangle Area

来源:互联网 发布:手机我的世界枪械js 编辑:程序博客网 时间:2024/04/30 18:14

Question

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.

Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.

Show Tags


Solution

Analysis

The coordination of the corner node of the intersection area is the the min and max of those coordinations.

Code

class Solution(object):    def computeArea(self, A, B, C, D, E, F, G, H):        """        :type A: int        :type B: int        :type C: int        :type D: int        :type E: int        :type F: int        :type G: int        :type H: int        :rtype: int        """        return (C-A)*(D-B) + (G-E)*(H-F) - max(0,min(C,G)-max(E,A))*max(0,min(D,H)-max(F,B))
0 0
原创粉丝点击