LeetCode 3047: Find the Largest Area of Square Inside Two Rectangles Solution

Master LeetCode problem 3047 (Find the Largest Area of Square Inside Two Rectangles), a medium 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.

3047. Find the Largest Area of Square Inside Two Rectangles

Problem Explanation

Explanation

To find the largest area of a square inside the intersecting region of at least two rectangles, we can iterate through all pairs of rectangles and find the intersection of their regions. For each pair of rectangles, we calculate the side length of the largest square that can fit inside their intersection. We keep track of the maximum area found so far and return it as the result. If no square can fit inside the intersection of any two rectangles, we return 0.

Algorithm:

  1. Iterate through all pairs of rectangles.
  2. Find the intersection of the two rectangles.
  3. Calculate the side length of the largest square that can fit inside the intersection.
  4. Update the maximum area found so far.
  5. Return the maximum area as the result.

Time Complexity: O(n^2) where n is the number of rectangles Space Complexity: O(1)

Solution Code

class Solution {
    public int largestArea(int[][] bottomLeft, int[][] topRight) {
        int maxArea = 0;
        for (int i = 0; i < bottomLeft.length; i++) {
            for (int j = i + 1; j < bottomLeft.length; j++) {
                int x1 = Math.max(bottomLeft[i][0], bottomLeft[j][0]);
                int y1 = Math.max(bottomLeft[i][1], bottomLeft[j][1]);
                int x2 = Math.min(topRight[i][0], topRight[j][0]);
                int y2 = Math.min(topRight[i][1], topRight[j][1]);
                
                if (x1 < x2 && y1 < y2) {
                    int side = Math.min(x2 - x1, y2 - y1);
                    maxArea = Math.max(maxArea, side * side);
                }
            }
        }
        return maxArea;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3047 (Find the Largest Area of Square Inside Two Rectangles)?

This page provides optimized solutions for LeetCode problem 3047 (Find the Largest Area of Square Inside Two Rectangles) 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 3047 (Find the Largest Area of Square Inside Two Rectangles)?

The time complexity for LeetCode 3047 (Find the Largest Area of Square Inside Two Rectangles) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3047 on DevExCode?

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

Back to LeetCode Solutions