Facebook Pixel

Union Area of Rectangles

You are given axis-aligned rectangles in rectangles, where rectangles[i] = [x1, y1, x2, y2] describes the half-open region from bottom-left corner (x1, y1) to top-right corner (x2, y2).

Return the total area covered by at least one rectangle. Overlapping regions count only once.

Toggle rectangles in the demo to see which unit cells belong to the union and how overlap is counted once.

Input & Output
Input
rectangles — a list of rectangles, each represented as [x1, y1, x2, y2] where (x1,y1) is the bottom-left corner and (x2,y2) is the top-right corner
Output
the total area covered by at least one rectangle
Example
Input
rectangles = [[1,1,4,3],[2,2,5,4]]
Output
10
Explanation
The rectangles have areas `6` and `6`, and their overlap has area `2`. Their union area is `6 + 6 - 2 = 10`.
Example
Input
rectangles = [[0,0,2,2],[2,0,4,2]]
Output
8
Explanation
The rectangles touch at `x = 2` but do not overlap, so both areas are included in full.
Example
Input
rectangles = [[0,0,3,3],[1,1,2,2]]
Output
9
Explanation
The smaller rectangle lies entirely inside the larger rectangle and adds no new covered area.
Constraints
  • 1 <= rectangles.length <= 200
  • rectangles[i].length == 4
  • 0 <= x1 < x2 <= 10^4
  • 0 <= y1 < y2 <= 10^4

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro