836. Rectangle Overlap
Explanation:
To determine if two rectangles overlap, we need to check if there exists an intersection area between the two rectangles. The rectangles are represented by their bottom-left and top-right coordinates. If the intersection area is positive, then the rectangles overlap.
The idea is to check for overlap by comparing the x and y coordinates of the rectangles. If there is an overlap along both x and y axes, then the rectangles overlap.
Algorithm:
- Check if the rectangles overlap along the x-axis by comparing the x-coordinates of the rectangles.
- Check if the rectangles overlap along the y-axis by comparing the y-coordinates of the rectangles.
- If there is overlap along both axes, return true; otherwise, return false.
Time Complexity: O(1) - The algorithm has a constant time complexity as it performs a fixed number of comparisons.
Space Complexity: O(1) - The algorithm uses a constant amount of extra space.
:
class Solution {
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
return (rec1[0] < rec2[2] && rec1[1] < rec2[3] && rec1[2] > rec2[0] && rec1[3] > rec2[1]);
}
}
Code Editor (Testing phase)
Improve Your Solution
Use the editor below to refine the provided solution. Select a programming language and try the following:
- Add import statement if required.
- Optimize the code for better time or space complexity.
- Add test cases to validate edge cases and common scenarios.
- Handle error conditions or invalid inputs gracefully.
- Experiment with alternative approaches to deepen your understanding.
Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.