Skip to content

Latest commit

 

History

History
57 lines (45 loc) · 1.12 KB

223.md

File metadata and controls

57 lines (45 loc) · 1.12 KB
  1. Rectangle Area
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.

my thoughts:

1. geometry.

my solution:

********** 
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
        """
        a1 = (C - A) * (D - B)
        a2 = (G - E) * (H - F)
        
        if E >= C or A >= G or F >= D or B >= H:
            return a1 + a2
        
        x1 = max(A, E)
        x2 = min(C, G)
        y1 = max(B, F)
        y2 = min(D, H)
        
        overlap = (x2 - x1) * (y2 - y1)
        
        return a1 + a2 - overlap

my comments:


from other ppl's solution:

1. N/A