Rectangle Overlap — LeetCode 836 Python Solution
- Problem
- #836
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
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
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
| Measure | Complexity |
|---|---|
| Time | O(1) |
| Space | O(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.