Skip to content
← All posts

Rectangle Area: Computing Overlap Between Two Rectangles

3 min read
leetcodeproblemmediummath

LeetCode 223, Rectangle Area, asks you to find the total area covered by two axis-aligned rectangles on a 2D plane. Each rectangle is defined by its bottom-left and top-right corners. The catch is that the rectangles may overlap, so you cannot simply add their areas together.

012345678910012345678A (1,1)-(5,5)B (3,2)-(8,7)overlap(1,1)(5,5)(3,2)(8,7)
Two rectangles on a coordinate plane. The dashed region shows where they overlap.

The inclusion-exclusion approach

You probably remember this idea from set theory: if two sets overlap, the size of their union equals the sum of their individual sizes minus the size of their intersection. The same principle applies here with areas.

Compute the area of each rectangle independently, then subtract the overlapping region (if any). The overlap is itself a rectangle whose boundaries come from the intersection of the two input rectangles along both axes.

For the overlap width, take the rightmost left edge and the leftmost right edge. If the result is negative, there is no horizontal overlap at all, and you clamp to zero. Same logic for the height: take the topmost bottom edge and the bottommost top edge.

The solution

class Solution:
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int,
                    bx1: int, by1: int, bx2: int, by2: int) -> int:
        area_a = (ax2 - ax1) * (ay2 - ay1)
        area_b = (bx2 - bx1) * (by2 - by1)

        overlap_w = max(0, min(ax2, bx2) - max(ax1, bx1))
        overlap_h = max(0, min(ay2, by2) - max(ay1, by1))
        overlap = overlap_w * overlap_h

        return area_a + area_b - overlap
  1. Area of rectangle A. Width times height using the difference between right and left x-coordinates, and top and bottom y-coordinates.
  2. Area of rectangle B. Same calculation for the second rectangle.
  3. Overlap width. min(ax2, bx2) - max(ax1, bx1) gives the horizontal span where both rectangles exist. Wrapping in max(0, ...) ensures you get zero when they do not overlap.
  4. Overlap height. Same idea along the y-axis.
  5. Combine. Add both areas and subtract the overlap to avoid double-counting.

Step-by-step walkthrough

Step 1: Compute area of rectangle A

4416(1,1)(5,5)

width = 5 - 1 = 4, height = 5 - 1 = 4, area = 16

Step 2: Compute area of rectangle B

5525(3,2)(8,7)

width = 8 - 3 = 5, height = 7 - 2 = 5, area = 25

Step 3: Compute overlap dimensions

236AB

overlap_w = max(0, min(5,8) - max(1,3)) = 2, overlap_h = max(0, min(5,7) - max(1,2)) = 3

Step 4: Apply inclusion-exclusion

16+25-6=35

total = 16 + 25 - 6 = 35

Complexity analysis

MetricValueWhy
TimeO(1)Fixed number of arithmetic operations regardless of input size
SpaceO(1)Only a handful of variables, no data structures

Building blocks

  • Inclusion-exclusion principle. Whenever you combine two sets (or regions, or counts) that might overlap, add them together and subtract the intersection. This appears in probability, combinatorics, and geometry problems alike.
  • Interval overlap detection. Checking whether two 1D intervals [a, b] and [c, d] intersect, and computing the intersection length as max(0, min(b, d) - max(a, c)), is a fundamental building block for sweep-line algorithms and rectangle problems.
  • Clamping with max(0, ...). A clean way to handle the "no overlap" case without branching. If the computed overlap is negative, it simply becomes zero.

Edge cases

  • No overlap at all. The rectangles are completely separated horizontally or vertically. The overlap width or height (or both) clamps to zero, so overlap area is zero.
  • One rectangle inside the other. The overlap equals the area of the smaller rectangle. The formula handles this naturally.
  • Touching edges only. Two rectangles share an edge but no interior area. The overlap dimension along that axis is zero.
  • Identical rectangles. Both areas are the same and the overlap equals that area, so the result is just one rectangle's area.
  • Large coordinate values. The problem guarantees 32-bit integers. In Python this is not an issue since integers have arbitrary precision, but in other languages you would need to watch for overflow.

When two axis-aligned rectangles overlap, their intersection is always another axis-aligned rectangle. You never need to handle irregular shapes, just compute each dimension of the overlap independently.

From understanding to recall

This problem tests a single clean idea: inclusion-exclusion applied to geometry. The tricky part is not the math itself but remembering exactly how to compute the overlap rectangle from two sets of coordinates. Practicing this once or twice with spaced repetition locks in the pattern so you can reproduce the four-line core of the solution from memory.

Related problems

  • Maximal Rectangle finds the largest rectangle of 1s in a binary matrix, building on rectangle geometry fundamentals.
  • Largest Rectangle in Histogram uses a stack to find the biggest rectangle under histogram bars, another geometry-meets-algorithm classic.
  • Set Matrix Zeroes requires tracking row and column boundaries in a matrix, sharing the coordinate reasoning you use here.