Python 0836.py
class Solution:
def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
def less(r1, r2):
return r1[0] < r2[2] and r1[1] < r2[3]
return less(rec1, rec2) and less(rec2, rec1)
Difficulty
and has an acceptance rate of about 47% (300k+ submissions).
Typical patterns to solve this problem:
- Geometry
- Math
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a Solution class with a single method isRectangleOverlap. The method determines whether two axis-aligned rectangles, each described by a list of four integers [x1, y1, x2, y2], share any interior area. The core logic is encapsulated in a nested helper function named less, which performs a pairwise comparison of the rectangle coordinates. The final result is obtained by invoking less in both directions and combining the outcomes with a logical conjunction.
Key Steps
The method begins by declaring an inner function less. This helper receives two rectangle specifications, r1 and r2, and evaluates two relational expressions:
def less(r1, r2):
return r1[0] < r2[2] and r1[1] < r2[3]
The first expression, r1[0] < r2[2], checks whether the left edge of r1 lies strictly to the left of the right edge of r2. The second expression, r1[1] < r2[3], verifies that the bottom edge of r1 is strictly below the top edge of r2. Both conditions must hold for less to return True.
After defining less, the method evaluates the overlap condition by calling the helper twice, swapping the rectangle arguments each time:
return less(rec1, rec2) and less(rec2, rec1)
The first call confirms that rec1 is positioned left-of and below rec2. The second call confirms the converse relationship. Only when both directional checks succeed does the method conclude that the rectangles overlap.
Edge Cases Handled
The strict inequality (<) used in the less function ensures that rectangles sharing only a border or a corner are not considered overlapping. When the right edge of one rectangle coincides exactly with the left edge of the other (r1[0] == r2[2]), the first relational test fails, yielding False. The same logic applies to top-bottom alignment (r1[1] == r2[3]). Consequently, the implementation correctly distinguishes between touching rectangles and those with a positive-area intersection.
Scenarios where one rectangle is entirely contained within the other also satisfy both directional checks. The inner rectangle’s left edge is left of the outer rectangle’s right edge, and its bottom edge is below the outer rectangle’s top edge; the reverse checks also hold, leading to a True result.
The code operates correctly for rectangles positioned in any quadrant of the Cartesian plane, including those with negative coordinate values, because the comparisons are purely relational and do not depend on sign.
Performance Optimizations
Embedding the less function inside isRectangleOverlap eliminates the need for external helper definitions and keeps the comparison logic localized, reducing function call overhead. The use of a single logical and expression leverages short-circuit evaluation: if the first less call returns False, the second call is never executed, saving unnecessary work in non-overlapping cases.
All operations performed-array indexing and integer comparisons-are constant-time and involve no allocation of additional data structures. This minimalistic approach results in a highly efficient execution path for the overlap test.
ELI5 - Simplified Explanation
Imagine two colorful building blocks sitting on a flat table. Each block has a special address written as four numbers that describe where it starts and how far it stretches. One block is called rec1 and the other is called rec2. The code uses a tiny helper tool called less to check if these two blocks are touching or overlapping in specific ways.
The first part of the helper checks if the first block starts before the second block ends. The second part makes sure the bottom edges have a relationship that allows them to share some space. By using the helper twice, the code looks at both blocks from different sides to see if they bump into each other. If the math shows that the space belonging to the first block and the space belonging to the second block share any of the same ground, then the blocks are overlapping.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| author | solutions | ||||||||
| www | AlgoMonster | C++ | Java | Python | TS | ||||
| WalkCCC | C++ | Java | Python | ||||||
| leetcode.ca | C++ | Java | Python | Go | TS | ||||
| LeetcodeHelp | C++ | Java | Python | ||||||
| MemoryLimitExceeded | C++ | Python | |||||||
| Progiez | C++ | Java | Python | ||||||
| github | BigEggStudy | C# | |||||||
| black-shadows | C++ | ||||||||
| charles-wangkai | Java | ||||||||
| everthis | JS | ||||||||
| fengvyi | C++ | ||||||||
| gouthampradhan | Java | ||||||||
| halfrost | Go | ||||||||
| JoshCrozier | JS | ||||||||
| keineahnung2345 | C++ | ||||||||
| lkwq007 | Python | ||||||||
| lzl124631x | C++ | ||||||||
| qiyuangong | Java | ||||||||
| ruoru | Rust | ||||||||
| shreymehul | C# | ||||||||
| suxrobGM | C# | ||||||||
| varunu28 | Java | ||||||||
| zfang399 | C++ | ||||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||||
| WalkCCC | C++, Python, Java | ||||||||
| leetcode.ca | C++, Go, Java, Python, TypeScript | ||||||||
| LeetcodeHelp | C++, Java, Python | ||||||||
| MemoryLimitExceeded | Python, C++ | ||||||||
| Progiez | C++, Java, Python | ||||||||
| BigEggStudy | C# | ||||||||
| black-shadows | C++, C++ | ||||||||
| charles-wangkai | Java | ||||||||
| everthis | JavaScript | ||||||||
| fengvyi | C++ | ||||||||
| gouthampradhan | Java | ||||||||
| halfrost | Go | ||||||||
| JoshCrozier | JavaScript | ||||||||
| keineahnung2345 | C++ | ||||||||
| lkwq007 | Python | ||||||||
| lzl124631x | C++ | ||||||||
| qiyuangong | Java | ||||||||
| ruoru | Rust | ||||||||
| shreymehul | C# | ||||||||
| suxrobGM | C# | ||||||||
| varunu28 | Java | ||||||||
| zfang399 | C++ |
Videos
Similar Problems
- problem 223: Rectangle Area
Solved On The Same Day
- problem 867: Transpose Matrix
- problem 2824: Count Pairs Whose Sum is Less than Target
- problem 475: Heaters
- problem 672: Bulb Switcher II
- problem 1461: Check If a String Contains All Binary Codes of Size K
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality