#836

Rectangle Overlap

Easy
MathGeometryGeometryCoordinate Systems
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(1)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

The optimal solution uses the same logic as the brute force but is more concise. We can combine the checks into a single return statement for clarity and efficiency.

⚙️

Algorithm

1 steps
  1. 1Step 1: Return true if the rectangles overlap, which is determined by checking if none of the conditions for non-overlapping rectangles are satisfied.
solution.py2 lines
1def isRectangleOverlap(rec1, rec2):
2    return not (rec1[2] <= rec2[0] or rec1[0] >= rec2[2] or rec1[3] <= rec2[1] or rec1[1] >= rec2[3])

Complexity note: The time complexity remains O(1) as we are still performing a fixed number of checks. The space complexity is O(1) since we are not using any additional data structures.

  • 1Rectangles overlap if they do not satisfy any of the non-overlapping conditions.
  • 2The conditions for non-overlapping rectangles are based on their edges.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.