Union Area of Two Rectangles
Problem statement
Two axis-aligned rectangles are described by their bottom-left and top-right corners: (ax1, ay1), (ax2, ay2) and (bx1, by1), (bx2, by2).
Return the total area covered by at least one rectangle.
Function
rectangleUnionArea(ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) → longExamples
Example 1
ax1 = -3ay1 = 0ax2 = 3ay2 = 4bx1 = 0by1 = -1bx2 = 9by2 = 2return = 45The rectangle areas are 24 and 27, and their overlap area is 6, giving 45.
Example 2
ax1 = 0ay1 = 0ax2 = 2ay2 = 2bx1 = 2by1 = 0bx2 = 4by2 = 2return = 8The rectangles touch at an edge but have zero overlap area.
Constraints
-10^9 <= ax1 < ax2 <= 10^9and-10^9 <= ay1 < ay2 <= 10^9.- The same strict corner ordering holds for rectangle B.
- The answer fits in a signed 64-bit integer.