Watch 10 video solutions for Rectangle Area, a medium level problem involving Math, Geometry. This walkthrough by NeetCode has 287,824 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).
The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (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
Constraints:
-104 <= ax1 <= ax2 <= 104-104 <= ay1 <= ay2 <= 104-104 <= bx1 <= bx2 <= 104-104 <= by1 <= by2 <= 104Problem Overview: Two axis-aligned rectangles are defined by their bottom-left and top-right coordinates. The task is to compute the total area covered by both rectangles. If the rectangles overlap, the overlapping region must only be counted once.
Approach 1: Calculate Areas Separately and Subtract Overlap (O(1) time, O(1) space)
Each rectangle’s area is computed independently using the standard formula (x2 - x1) * (y2 - y1). Adding these two values double-counts any overlapping region. The fix is to compute the overlap width and height using coordinate comparisons: overlapWidth = max(0, min(ax2, bx2) - max(ax1, bx1)) and overlapHeight = max(0, min(ay2, by2) - max(ay1, by1)). Multiply them to get the overlapping area and subtract it from the sum of both rectangles. This solution relies purely on coordinate arithmetic, making it extremely efficient with constant O(1) time and O(1) space. The logic mainly uses geometric reasoning from Math and coordinate comparisons from Geometry.
The key insight is that overlap occurs only when projections on both the x-axis and y-axis intersect. Using min and max isolates the intersection boundaries. The max(0, ...) guard prevents negative values when rectangles do not intersect. This approach is compact, easy to implement, and handles all cases including disjoint rectangles, partial overlap, and one rectangle fully inside another.
Approach 2: Coordinate Sweep Method (O(1) time, O(1) space)
A coordinate sweep views the rectangles as vertical spans along the x-axis. First determine the combined x-intervals where rectangles exist. For each overlapping x segment, compute the active y coverage by intersecting the rectangles’ vertical ranges. The covered area becomes width * unionHeight. While sweep-line techniques usually appear in problems involving many rectangles, the same idea applies here conceptually with just two rectangles.
This approach emphasizes interval reasoning rather than direct subtraction. You determine horizontal overlap first, then compute the effective vertical span that contributes to area. Although the final complexity is still O(1), it mirrors how large-scale rectangle union problems are solved with sweep-line algorithms and interval merging. Understanding this method strengthens intuition for more advanced geometry problems involving multiple regions.
Recommended for interviews: The expected solution is the area calculation with overlap subtraction. Interviewers want to see you identify rectangle intersection using min/max comparisons and avoid negative overlap values. A sweep-style explanation demonstrates deeper geometric thinking, but the direct arithmetic approach shows the cleanest reasoning and implementation under time pressure.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Calculate Areas Separately and Subtract Overlap | O(1) | O(1) | Best general solution for two rectangles. Simple coordinate math and minimal implementation. |
| Coordinate Sweep Method | O(1) | O(1) | Useful for understanding how rectangle union problems scale to many rectangles using sweep-line techniques. |