




Sponsored
Sponsored
This approach checks if there are conditions that confirm non-overlapping rectangles. Specifically, two rectangles do not overlap if:
If any of these conditions are true, the rectangles do not overlap. Otherwise, they do overlap.
Time Complexity: O(1), since the operation involves a constant number of comparisons.
Space Complexity: O(1), since the space used by the function is constant with respect to inputs.
1def isRectangleOverlap(rec1, rec2):
2    return not (rec1[2] <= rec2[0] or  
3                rec1[0] >= rec2[2]The function checks whether any of the non-overlapping conditions are true. If none are true, it means the rectangles overlap.
This approach computes the potential intersecting rectangle and verifies if its calculated dimensions form a valid rectangle with a positive area. The logic involves:
max(rec1[0], rec2[0])min(rec1[2], rec2[2])max(rec1[1], rec2[1])min(rec1[3], rec2[3])Time Complexity: O(1)
Space Complexity: O(1)
1    bool x_overlap = min(rec1[2], rec2[2]) > max(rec1[0], rec2[0]);
    bool y_overlap = min(rec1[3], rec2[3]) > max(rec1[1], rec2[1]);
    return x_overlap && y_overlap;
}This checks for overlapping x and y dimensions by ensuring the computed intersection has positive width and height.