↓Skip to main content
  1. LeetCode/

LeetCode 836: Rectangle Overlap

·2 mins· ·
LeetCode Easy Math Geometry
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-15

  • Submissions: Wrong Answer → Runtime Error → Wrong Answer ×4 → Runtime Error ×2 → Wrong Answer ×4 → Accepted

Approach
#

2D overlap splits into two 1D interval overlaps (Math Template 3): the intersection is min(x2) - max(x1) wide and min(y2) - max(y1) tall, and both must be positive (touching edges do not count). No case analysis of who is left of whom is needed.

My Attempts
#

This took 13 submissions; the first twelve did case analysis on “some endpoint lies inside the other rectangle’s range” and always missed a case:

  • Checking only that rec2 starts before rec1 ends: [1, 2, 3, 4] and [2, 1, 4, 2] merely touch at y = 2, yet it returned True.
  • Checking only y, or with strict inequalities: identical y ranges ([1, 0, 4, 2], [3, 0, 4, 2]) returned False.
  • With endpoint checks on both axes, containment slipped through: [3, 3, 4, 4] lies inside [3, 2, 6, 5], and it returned False.

Computing the intersection’s width and height finally passed.

Solution
#

class Solution:
    def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
        width = min(rec1[2], rec2[2]) - max(rec1[0], rec2[0])
        height = min(rec1[3], rec2[3]) - max(rec1[1], rec2[1])
        return width > 0 and height > 0

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • Check both the x and the y direction.
  • Use strict inequality; touching edges do not count.
  • When one rectangle contains the other, neither endpoint falls inside the other’s range, so endpoint-in-interval checks miss it.

Interview Explanation
#

I split the overlap into x and y intervals: the intersection width is the smaller right edge minus the larger left edge, likewise for height, and both must be positive. Time O(1).

Related

LeetCode 812: Largest Triangle Area
·2 mins
LeetCode Easy Array Math Geometry Polygons
LeetCode 812 note: with at most 50 points, enumerate every triple and take the largest shoelace area
LeetCode 728: Self Dividing Numbers
·2 mins
LeetCode Easy Math
LeetCode 728 note: check every number in the range; each digit must be nonzero and divide the number
LeetCode 628: Maximum Product of Three Numbers
·1 min
LeetCode Easy Array Math Sorting
LeetCode 628 note: after sorting there are only two candidates: the three largest, or the two smallest (negatives) times the largest
LeetCode 598: Range Addition II
·2 mins
LeetCode Easy Array Math
LeetCode 598 note: every operation covers the top-left corner, so the most-incremented cells form the intersection of all operations: min(a) × min(b)
LeetCode 504: Base 7
·1 min
LeetCode Easy Math String
LeetCode 504 note: repeatedly take % 7 and // 7 of the absolute value, then add the sign and reverse; handle 0 separately
LeetCode 509: Fibonacci Number
·1 min
LeetCode Easy Math Dynamic-Programming Recursion Memoization
LeetCode 509 note: the starting point of 1D DP; only the previous two terms are needed, so roll two variables