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.
rectangles = [[1,1,4,3],[2,2,5,4]]
10
The rectangles have areas 6 and 6, and their overlap has area 2. Their union area is 6 + 6 - 2 = 10.
rectangles = [[0,0,2,2],[2,0,4,2]]
8
The rectangles touch at x = 2 but do not overlap, so both areas are included in full.
rectangles = [[0,0,3,3],[1,1,2,2]]
9
The smaller rectangle lies entirely inside the larger rectangle and adds no new covered area.
- 1 <= rectangles.length <= 200
- rectangles[i].length == 4
- 0 <= x1 < x2 <= 10^4
- 0 <= y1 < y2 <= 10^4