Practice/Google/Leetcode 223. Rectangle Area
CodingOptional
You are given the coordinates of two axis-aligned rectangles in a 2D plane. Each rectangle is defined by its bottom-left corner and top-right corner coordinates.
For the first rectangle, you receive: (ax1, ay1) as the bottom-left corner and (ax2, ay2) as the top-right corner.
For the second rectangle, you receive: (bx1, by1) as the bottom-left corner and (bx2, by2) as the top-right corner.
Calculate the total area covered by the two rectangles combined. If the rectangles overlap, count the overlapping region only once.
-10^4 <= ax1 < ax2 <= 10^4-10^4 <= ay1 < ay2 <= 10^4-10^4 <= bx1 < bx2 <= 10^4-10^4 <= by1 < by2 <= 10^4Example 1:
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2 Output: 45 Explanation: First rectangle has area (3-(-3)) * (4-0) = 24. Second rectangle has area (9-0) * (2-(-1)) = 27. They overlap in the region from (0, 0) to (3, 2), which has area 6. Total area = 24 + 27 - 6 = 45.
Example 2:
Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = 3, by1 = 3, bx2 = 7, by2 = 7 Output: 32 Explanation: First rectangle has area 16, second has area 16. They don't overlap at all, so total area = 16 + 16 = 32.
Example 3:
Input: ax1 = 0, ay1 = 0, ax2 = 5, ay2 = 5, bx1 = 0, by1 = 0, bx2 = 5, by2 = 5 Output: 25 Explanation: Both rectangles are identical with area 25. They completely overlap, so total area = 25.