Rectangle Overlap — LeetCode 836 Python Solution

EasyGeometryMath
Problem
#836
Reading time
2 min

The problem

An axis-aligned rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) is the coordinate of its bottom-left corner, and (x2, y2) is the coordinate of its top-right corner. Its top and bottom edges are parallel to the X-axis, and its left and right edges are parallel to the Y-axis.

Example

Input
rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output
true

Python solution

Python
class Solution:
    def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
        x1, y1, x2, y2 = rec1
        x3, y3, x4, y4 = rec2
        return not (y3 >= y2 or y4 <= y1 or x3 >= x2 or x4 <= x1)

Complexity

MeasureComplexity
TimeO(1)
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 836. Rectangle Overlap is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math and Geometry.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 836. Rectangle Overlap?
LeetCode 836. Rectangle Overlap is rated Easy on LeetCode.
What is the time complexity of LeetCode 836. Rectangle Overlap?
The Python solution on this page runs in O(1).
What is the space complexity of LeetCode 836. Rectangle Overlap?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 836. Rectangle Overlap cover?
LeetCode 836. Rectangle Overlap is tagged Geometry and Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview