0% completed
Problem Statement
You are given a rectangle 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. The top
and bottom
edges of the rectangle are parallel to the X-axis
, and the left and right edges are parallel to the Y-axis
.
Two rectangles overlap
if the rectangles share a part of the area
; mere touching at edges
or corners
doesn't count.
Given two rectangles rec1
and rec2
, return true
if they overlap
, otherwise return false
.
Examples
-
Example 1:
- Input:
rec1 = [0, 0, 1, 1], rec2 = [1, 1, 2, 2]
- Expected Output:
false
- Justification: The two rectangles touch at a corner but do not share any area, thus they do not overlap.
- Input:
-
Example 2:
- Input:
rec1 = [0, 0, 2, 2], rec2 = [1, 1, 3, 3]
- Expected Output:
true
- Justification: The rectangles share a part of their area, thus they overlap.
- Input:
-
Example 3:
- Input:
rec1 = [1, 1, 3, 3], rec2 = [2, 2, 4, 4]
- Expected Output:
true
- Justification: These rectangles share a central area, thereby confirming overlap.
- Input:
Solution
To solve this problem, we approach it by checking the conditions under which rectangles do not overlap, which simplifies the problem significantly. We know that if one rectangle is to the left, right, above, or below the other, they do not overlap. This method is effective because it reduces the problem to simple comparisons of the x and y coordinates of the rectangles' corners.
Our approach involves checking if the maximum of the bottom-left x-coordinates is less than the minimum of the top-right x-coordinates and if the maximum of the bottom-left y-coordinates is less than the minimum of the top-right y-coordinates. If both conditions are true, the rectangles overlap. This method is both simple and efficient, avoiding unnecessary calculations and complexities involved in area computation or edge/corner cases.
Step-by-Step Algorithm
-
Check Left: Verify if the right edge (
x2
value) of the first rectangle (rec1
) is to the left of the left edge (x1
value) of the second rectangle (rec2
). This is done by comparing ifrec1[2] <= rec2[0]
. -
Check Right: Determine if the left edge (
x1
value) ofrec1
is to the right of the right edge (x2
value) ofrec2
. This involves checking ifrec1[0] >= rec2[2]
. -
Check Below: Check if the top edge (
y2
value) ofrec1
is below the bottom edge (y1
value) ofrec2
by comparing ifrec1[3] <= rec2[1]
. -
Check Above: Ascertain if the bottom edge (
y1
value) ofrec1
is above the top edge (y2
value) ofrec2
. This is done by verifying ifrec1[1] >= rec2[3]
. -
Return Result: If none of the above conditions are true, it means that
rec1
andrec2
overlap. Returntrue
in this case. If any of the conditions are met, it implies that the rectangles do not overlap; hence, returnfalse
.
Algorithm Walkthrough with Example
Given rectangles rec1 = [1, 1, 3, 3]
and rec2 = [2, 2, 4, 4]
:
-
Check Left: Compare if the right edge of
rec1
(3
) is to the left of the left edge ofrec2
(2
). This is not true since3
is not less than or equal to2
. -
Check Right: Determine if the left edge of
rec1
(1
) is to the right of the right edge ofrec2
(4
). This condition is false because1
is not greater than or equal to4
. -
Check Below: Check if the top edge of
rec1
(3
) is below the bottom edge ofrec2
(2
). This is false since3
is not less than or equal to2
. -
Check Above: Ascertain if the bottom edge of
rec1
(1
) is above the top edge ofrec2
(4
). This condition is also false as1
is not greater than or equal to4
. -
Return Result: Since none of the conditions for being to the left, right, above, or below are met, the rectangles must overlap. Therefore, for
rec1 = [1, 1, 3, 3]
andrec2 = [2, 2, 4, 4]
, the algorithm returnstrue
, indicating that the rectangles overlap.
Code
Complexity Analysis
-
Time Complexity: O(1)
- The time complexity of this algorithm is constant, O(1), because the computation to determine if two rectangles overlap involves a fixed number of comparisons. No matter the input size or the specific values of the rectangle coordinates, the number of operations remains the same.
-
Space Complexity: O(1)
- The space complexity is also constant, O(1), as the algorithm operates directly on the input parameters without utilizing any additional data structures that scale with input size.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible