Leetcode 836: Rectangle Overlap

grid47
grid47
Exploring patterns and algorithms
Aug 15, 2024 5 min read

You are given two axis-aligned rectangles represented by two lists, rec1 and rec2. Each list contains four integers: [x1, y1, x2, y2], where (x1, y1) represents the bottom-left corner and (x2, y2) represents the top-right corner of the rectangle. You need to determine if the two rectangles overlap. Two rectangles overlap if their intersection area is positive. Rectangles that only touch at the edges or corners do not count as overlapping.
Problem
Approach
Steps
Complexity
Input: The input consists of two lists representing two axis-aligned rectangles. Each list contains four integers: [x1, y1, x2, y2].
Example: Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Constraints:
• rec1.length == 4
• rec2.length == 4
• -10^9 <= rec1[i], rec2[i] <= 10^9
• Both rec1 and rec2 represent valid rectangles with a non-zero area.
Output: Return true if the two rectangles overlap, otherwise return false.
Example: Output: true
Constraints:
• The output should be a boolean value indicating whether the rectangles overlap.
Goal: The goal is to check if there is a positive intersection area between the two given rectangles.
Steps:
• Step 1: Compare the horizontal and vertical ranges of both rectangles.
• Step 2: Check if there is no overlap by ensuring the rectangles do not meet in either direction (horizontal or vertical).
• Step 3: Return true if there is an overlap, otherwise return false.
Goal: Ensure that both rectangles are valid and the area is non-zero.
Steps:
• Ensure the rectangles are axis-aligned and the coordinates form a valid rectangle.
Assumptions:
• Both rec1 and rec2 represent valid axis-aligned rectangles.
Input: Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Explanation: These two rectangles overlap because their intersection has a positive area.

Input: Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Explanation: These rectangles do not overlap because they only touch at the edges, not overlapping in area.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus