Rectangle Overlap (LeetCode 836): Solution and Explanation

Easy
Math
Geometry

Each rectangle is given as [x1, y1, x2, y2] with its bottom-left and top-right corners, and its sides run parallel to the axes. Return true if the two rectangles share a region with positive area. Touching along an edge or at a corner does not count.

Intuition

It is tempting to test whether a corner of one rectangle lies inside the other. That misses the cross shape, where a wide flat rectangle and a tall thin one overlap in the middle but no corner of either lies inside the other.

A cleaner view is to look at shadows. Shine a light down onto the x axis and each rectangle casts an interval [x1, x2]. Shine it from the side and each casts [y1, y2] on the y axis. The intersection of two axis-aligned rectangles is the rectangle whose x range is the overlap of the x shadows and whose y range is the overlap of the y shadows.

So the problem splits into two copies of a simpler one: do two intervals overlap with positive length? The shared part of [a1, a2] and [b1, b2] starts at the later start, max(a1, b1), and ends at the earlier end, min(a2, b2). It has positive length exactly when the start is strictly less than the end.

The strict < is what makes edge contact return false. If one rectangle ends at x = 1 and the other starts at x = 1, the shared x range is the single point 1, with zero width, so the area is zero.

Approach: Interval overlap on both axes

  1. Set left = max(rec1[0], rec2[0]) and right = min(rec1[2], rec2[2]). These bound the shared x range.
  2. The x shadows overlap with positive width if left < right.
  3. Set bottom = max(rec1[1], rec2[1]) and top = min(rec1[3], rec2[3]). These bound the shared y range.
  4. The y shadows overlap with positive height if bottom < top.
  5. Return true only when both checks hold.

Step-by-step example

rec1 = [0,0,2,2], rec2 = [1,1,3,3]
  1. left = max(0, 1) = 1 and right = min(2, 3) = 2.
  2. 1 < 2, so the x shadows overlap on [1, 2].
  3. bottom = max(0, 1) = 1 and top = min(2, 3) = 2.
  4. 1 < 2, so the y shadows overlap on [1, 2].
  5. Both axes overlap, so the rectangles share the unit square from (1, 1) to (2, 2). Return true.

Code: Python, Java, C++

1class Solution:
2    def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
3        left = max(rec1[0], rec2[0])
4        right = min(rec1[2], rec2[2])
5        x_overlap = left < right
6        bottom = max(rec1[1], rec2[1])
7        top = min(rec1[3], rec2[3])
8        y_overlap = bottom < top
9        return x_overlap and y_overlap
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1)

The answer comes from four comparisons and two more checks, no matter how large the coordinates are.

Space

O(1)

Only four integers are stored.

Other approaches

Rule out the separating cases (time O(1), space O(1))

The rectangles do not overlap when one is fully left of, right of, above or below the other. Return the negation: not (rec1[2] <= rec2[0] or rec2[2] <= rec1[0] or rec1[3] <= rec2[1] or rec2[3] <= rec1[1]). It is the same test written from the other side.

Compute the intersection area (time O(1), space O(1))

Compute max(0, right - left) * max(0, top - bottom) and check that it is positive. It works, but the product of two large widths can overflow a 32-bit integer in Java, C++ or C#, so comparing is safer than multiplying.

Common mistakes

  • Using <= instead of <, which reports rectangles that only share an edge or a corner as overlapping.
  • Checking only whether a corner of one rectangle lies inside the other. The cross shape overlaps with no corner inside.
  • Computing right - left or the area in 32-bit integers. With coordinates up to 10^9 the difference can reach 2 * 10^9 and overflow, while plain comparisons never do.
  • Mixing up the index order: indices 0 and 2 are x values, indices 1 and 3 are y values.

Frequently asked questions

Why is it enough to check the two axes separately?

For axis-aligned rectangles, a point lies in both rectangles exactly when its x coordinate lies in both x ranges and its y coordinate lies in both y ranges. So the intersection is the product of the two interval overlaps, and its area is positive only when both overlaps have positive length.

Do rectangles that touch at a corner overlap?

No. Touching at a corner makes both shared ranges a single point, so the area is zero. The strict comparisons return false for that case.

Does this work if one rectangle is inside the other?

Yes. The inner rectangle's edges become left, right, bottom and top, so both checks pass and the function returns true.

What is the time complexity of Rectangle Overlap?

O(1) time and O(1) space, since it only compares a fixed number of coordinates.