Problem Description
Given two axis-aligned rectangles rec1 and rec2, where each is represented as a list [x1, y1, x2, y2] (with (x1, y1) representing the bottom-left corner and (x2, y2) representing the top-right corner), determine if the two rectangles overlap. Two rectangles overlap if and only if the area of their intersection is positive (i.e., they are not merely touching at the edges or corners).
Key Insights
- Two rectangles do not overlap if one is completely to the left or to the right of the other.
- Two rectangles do not overlap if one is completely above or below the other.
- By checking the non-overlapping conditions and taking the negation, you can determine if there is an overlap.
Space and Time Complexity
Time Complexity: O(1) – Only constant time checks are performed. Space Complexity: O(1) – Only a fixed amount of extra space is used.
Solution
The solution checks if one rectangle is entirely to the left of the other by comparing the x-coordinates, and similarly if one is entirely above or below the other by comparing the y-coordinates. If any of these non-overlapping conditions are true, the rectangles do not overlap and the answer is false. Otherwise, they overlap and the answer is true.
Data structures used: Basic integer comparisons. Algorithmic approach: Conditional checks based on the positional relationships of the rectangles.