Rectangle Area II - Problem
Rectangle Area II
Imagine you're an urban planner looking at a city map with overlapping building plots. You need to calculate the total land area covered by all buildings, but here's the catch: overlapping areas should only be counted once!
You are given a 2D array of
Goal: Calculate the total area covered by all rectangles in the plane. Any area covered by two or more rectangles should only be counted once.
Output: Return the total area modulo
Example: If you have rectangles [[0,0,2,2], [1,0,2,3], [1,0,3,1]], the total unique area would be 6 (not 4+2+2=8 due to overlaps).
Imagine you're an urban planner looking at a city map with overlapping building plots. You need to calculate the total land area covered by all buildings, but here's the catch: overlapping areas should only be counted once!
You are given a 2D array of
rectangles where each rectangle[i] = [x1, y1, x2, y2] represents an axis-aligned rectangle. The coordinates (x1, y1) define the bottom-left corner and (x2, y2) define the top-right corner.Goal: Calculate the total area covered by all rectangles in the plane. Any area covered by two or more rectangles should only be counted once.
Output: Return the total area modulo
10^9 + 7 since the answer may be very large.Example: If you have rectangles [[0,0,2,2], [1,0,2,3], [1,0,3,1]], the total unique area would be 6 (not 4+2+2=8 due to overlaps).
Input & Output
example_1.py โ Basic Overlapping Rectangles
$
Input:
rectangles = [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
โบ
Output:
6
๐ก Note:
Rectangle 1: area = (2-0) * (2-0) = 4. Rectangle 2: area = (2-1) * (3-0) = 3. Rectangle 3: area = (3-1) * (1-0) = 2. Total individual areas = 4 + 3 + 2 = 9. However, overlapping regions should only be counted once. The total unique area covered is 6.
example_2.py โ Single Rectangle
$
Input:
rectangles = [[0,0,1000000000,1000000000]]
โบ
Output:
49
๐ก Note:
Single rectangle with area 10^18. Since we return modulo 10^9 + 7, the answer is (10^18) % (10^9 + 7) = 49.
example_3.py โ No Overlapping Rectangles
$
Input:
rectangles = [[0,0,1,1],[2,2,3,3],[4,4,5,5]]
โบ
Output:
3
๐ก Note:
Three non-overlapping unit squares. Each has area 1, so total area is 1 + 1 + 1 = 3.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Key Boundaries
Mark all unique x-coordinates where building edges occur
2
Create Survey Events
Plan when to start and stop measuring each building as you sweep vertically
3
Sweep Through City
Move horizontally, tracking which buildings are currently active
4
Calculate Total Area
For each strip, find the union of active building widths and multiply by height
Key Takeaway
๐ฏ Key Insight: Instead of checking every coordinate point (which could be 10ยนโธ points!), we use coordinate compression to only work with the important boundary points, then sweep efficiently to calculate the exact union area.
Time & Space Complexity
Time Complexity
O(nยฒ log n)
nยฒ for processing intervals at each sweep line, log n for sorting
โ Quadratic Growth
Space Complexity
O(n)
Space for compressed coordinates and events
โก Linearithmic Space
Constraints
- 1 โค rectangles.length โค 200
- rectangles[i].length = 4
- 0 โค xi1, yi1, xi2, yi2 โค 109
- xi1 < xi2 and yi1 < yi2 (valid rectangles)
- Answer fits in 32-bit integer after modulo operation
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code