#223

Rectangle Area

specialist · 945 · lc medium +32 · verified · 49% accepted · 2,146 likes · top 35%

play →

Description

Two axis-aligned rectangles lie in a 2D plane. Return the total area they cover together (counting overlapping area only once).

Rectangle A spans from (ax1, ay1) (bottom-left) to (ax2, ay2) (top-right). Rectangle B spans from (bx1, by1) to (bx2, by2).

Example 1:

Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output: 45

Example 2:

Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output: 16

Code

1
2
3