Rectangle Area (LeetCode 223): Solution and Explanation

Medium
Math
Geometry

Given two axis-aligned rectangles by their bottom-left and top-right corners, return the total area the two of them cover together.

Intuition

If the rectangles never touched, the answer would just be the sum of their areas. When they do overlap, that sum counts the shared region twice, so it has to be subtracted once. This is inclusion-exclusion: |A ∪ B| = |A| + |B| - |A ∩ B|.

The shared region of two axis-aligned rectangles is itself an axis-aligned rectangle. Its width is how much the two x-ranges overlap and its height is how much the two y-ranges overlap, so a 2D problem splits into two 1D ones.

Two intervals [l1, r1] and [l2, r2] overlap from the later start, max(l1, l2), to the earlier end, min(r1, r2). If that end is not past the start, they do not overlap, and wrapping the difference in max(0, ...) turns that case into a length of 0.

With the given limits each area is at most 4 * 10^8 and the sum at most 8 * 10^8, which still fits in a 32-bit int, so no 64-bit arithmetic is needed.

Approach: Inclusion-exclusion with interval overlap

  1. Compute area_a = (ax2 - ax1) * (ay2 - ay1) and area_b = (bx2 - bx1) * (by2 - by1).
  2. Compute the x-overlap overlap_w = max(0, min(ax2, bx2) - max(ax1, bx1)).
  3. Compute the y-overlap overlap_h = max(0, min(ay2, by2) - max(ay1, by1)).
  4. Return area_a + area_b - overlap_w * overlap_h.

Step-by-step example

ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
  1. A is 6 wide and 4 tall, so area_a = 24.
  2. B is 9 wide and 3 tall, so area_b = 27.
  3. On the x-axis, [-3, 3] and [0, 9] share [0, 3], so overlap_w = 3.
  4. On the y-axis, [0, 4] and [-1, 2] share [0, 2], so overlap_h = 2.
  5. The shared rectangle is 3 x 2 = 6, so the total is 24 + 27 - 6 = 45.

Code: Python, Java, C++

1class Solution:
2    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
3        area_a = (ax2 - ax1) * (ay2 - ay1)
4        area_b = (bx2 - bx1) * (by2 - by1)
5        overlap_w = max(0, min(ax2, bx2) - max(ax1, bx1))
6        overlap_h = max(0, min(ay2, by2) - max(ay1, by1))
7        return area_a + area_b - overlap_w * overlap_h
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1)

A fixed number of subtractions, comparisons and multiplications.

Space

O(1)

Only four integer variables.

Other approaches

Explicit overlap test first (time O(1), space O(1))

Check whether the rectangles are disjoint (one is entirely left, right, above or below the other) and return the plain sum in that case; otherwise compute the intersection corners. Same result, more branches.

Unit-cell counting (time O(area), space O(area))

Mark every unit square covered by either rectangle in a set and count them. Correct but O(area), which is up to 8 * 10^8 cells here, far too slow.

Common mistakes

  • Forgetting the max(0, ...) clamp. When the x-ranges do not overlap, min - max is negative; if the y-ranges also miss, two negatives multiply into a positive and the answer is wrong.
  • Clamping only the final product instead of each side. A negative width times a positive height gives a negative overlap that gets added instead of subtracted.
  • Treating rectangles that only touch along an edge as overlapping. Their overlap width or height is 0, so nothing is subtracted, which is correct.
  • Subtracting the overlap twice, or not at all, when the rectangles are identical or one contains the other.

Frequently asked questions

What if one rectangle is entirely inside the other?

The overlap is then the smaller rectangle itself, so the formula subtracts the smaller area once and returns the larger area, as expected.

Can a rectangle have zero area?

Yes. The constraints allow ax1 == ax2, which gives a degenerate rectangle of area 0. Its overlap width is also 0, so it contributes nothing.

Does this extend to more than two rectangles?

Inclusion-exclusion grows exponentially with the count. For many rectangles use a sweep line over x with a segment tree on y, as in Rectangle Area II.