Rectangle Overlap - Complete Solution Guide
Rectangle Overlap is LeetCode problem 836, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. Two rectangles overlap if the area of their intersection is positive . To be clear, two rectangles that only touch at the corner or edges do not overlap. Given two axis-aligned rectangles rec1 and rec2 ,
Detailed Explanation
The problem asks us to determine if two axis-aligned rectangles overlap. Each rectangle is defined by the coordinates of its bottom-left and top-right corners: `[x1, y1, x2, y2]`. Overlap is defined strictly; touching at edges or corners does *not* count as overlapping. The input consists of two such rectangle representations, `rec1` and `rec2`, and the output should be `true` if they overlap, and `false` otherwise.
Solution Approach
The solution approach is to check for the conditions under which the two rectangles *do not* overlap. Specifically, rectangle 1 is to the left of rectangle 2, rectangle 1 is to the right of rectangle 2, rectangle 1 is above rectangle 2, or rectangle 1 is below rectangle 2. If none of these conditions are met, then the rectangles must overlap.
Step-by-Step Algorithm
- Step 1: Extract the coordinates of the first rectangle (rec1) as x1, y1, x2, y2.
- Step 2: Extract the coordinates of the second rectangle (rec2) as x3, y3, x4, y4.
- Step 3: Check if rec1 is to the left of rec2: x2 <= x3. If true, they don't overlap.
- Step 4: Check if rec1 is to the right of rec2: x1 >= x4. If true, they don't overlap.
- Step 5: Check if rec1 is below rec2: y2 <= y3. If true, they don't overlap.
- Step 6: Check if rec1 is above rec2: y1 >= y4. If true, they don't overlap.
- Step 7: If none of the above conditions are true, return true (they overlap). Otherwise, return false (they don't overlap).
Key Insights
- Insight 1: The key is to identify the conditions under which the rectangles do *not* overlap. If we can find any of these conditions to be true, we can conclude that the rectangles don't overlap. The opposite of these non-overlapping conditions would then indicate overlap.
- Insight 2: The problem can be solved with simple comparisons between the coordinates of the rectangles. No complex data structures or algorithms are needed.
- Insight 3: Ensure that x1 < x2 and y1 < y2 for both rectangles, representing valid rectangles with positive area. The problem states that the given inputs represent a valid rectangle, but we should conceptually keep this in mind.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(1)
Topics
This problem involves: Math, Geometry.
Companies
Asked at: Nvidia, Qualcomm.