LeetCode 836: Rectangle Overlap Solution

Master LeetCode problem 836 (Rectangle Overlap), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

836. Rectangle Overlap

Problem Explanation

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:

  1. Check if the rectangles overlap along the x-axis by comparing the x-coordinates of the rectangles.
  2. Check if the rectangles overlap along the y-axis by comparing the y-coordinates of the rectangles.
  3. 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.

:

Solution Code

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]);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 836 (Rectangle Overlap)?

This page provides optimized solutions for LeetCode problem 836 (Rectangle Overlap) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 836 (Rectangle Overlap)?

The time complexity for LeetCode 836 (Rectangle Overlap) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 836 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 836 in Java, C++, or Python.

Back to LeetCode Solutions