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 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
๐Ÿ™๏ธ Smart City Planning with Line Sweep๐Ÿ“ Sweep Linexโ‚xโ‚‚xโ‚ƒxโ‚„xโ‚…xโ‚†xโ‚‡xโ‚ˆ๐ŸŽฏ Current Status: Measuring active building widths at sweep lineActive intervals: [xโ‚,xโ‚ƒ], [xโ‚„,xโ‚†], [xโ‚‡,xโ‚ˆ] โ†’ Total width = (xโ‚ƒ-xโ‚) + (xโ‚†-xโ‚„) + (xโ‚ˆ-xโ‚‡)Area contribution = Total width ร— Height of current segment
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

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for compressed coordinates and events

n
2n
โšก 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
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18
31.2K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen