LeetCode: Rectangle Area Posted on February 24, 2018July 26, 2020 by braindenny Rectangle Area Similar Problems: Meeting Rooms Rectangle Overlap CheatSheet: Leetcode For Code Interview CheatSheet: Common Code Problems & Follow-ups Tag: #geometry, #rectangle 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. Github: code.dennyzhang.com Credits To: leetcode.com Leave me comments, if you have better ways to solve. ## https://code.dennyzhang.com/rectangle-area ## Basic Ideas: ## width: min(C,G)-max(A,E) ## height: min(D, H)-max(B,F) ## ## If width or height is not positive, they don't overlap ## ## Complexity: Time O(1), Space O(1) class Solution: 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 """ area1 = abs(C-A)*abs(B-D) area2 = abs(E-G)*abs(F-H) w = min(C,G)-max(A,E) h = min(D, H)-max(B,F) if w<=0 or h<=0: return area1 + area2 else: return area1 + area2 - w*h Post Views: 11