LeetCode 3382: Maximum Area Rectangle With Point Constraints II Solution

Master LeetCode problem 3382 (Maximum Area Rectangle With Point Constraints II), a hard 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.

3382. Maximum Area Rectangle With Point Constraints II

Problem Explanation

Explanation

To solve this problem, we can iterate over all pairs of points and consider them as potential diagonal corners of our rectangle. For each pair of points, we calculate the other two corners by finding the points with the same x-coordinate and y-coordinate as the selected diagonal points. Then, we check if all 4 corners are distinct and no other points lie inside or on the border of the rectangle. We calculate the area of the rectangle and update the maximum area found so far. Finally, we return the maximum area.

  • Time complexity: O(n^2) where n is the number of points.
  • Space complexity: O(n) for storing the points.

Solution Code

class Solution {
    public int minAreaRect(int[] xCoord, int[] yCoord) {
        int n = xCoord.length;
        Map<Integer, Set<Integer>> points = new HashMap<>();
        
        for (int i = 0; i < n; i++) {
            points.computeIfAbsent(xCoord[i], k -> new HashSet<>()).add(yCoord[i]);
        }
        
        int maxArea = -1;
        
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (xCoord[i] != xCoord[j] && yCoord[i] != yCoord[j]) {
                    if (points.get(xCoord[i]).contains(yCoord[j]) && points.get(xCoord[j]).contains(yCoord[i])) {
                        maxArea = Math.max(maxArea, Math.abs(xCoord[j] - xCoord[i]) * Math.abs(yCoord[j] - yCoord[i]));
                    }
                }
            }
        }
        
        return maxArea;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3382 (Maximum Area Rectangle With Point Constraints II)?

This page provides optimized solutions for LeetCode problem 3382 (Maximum Area Rectangle With Point Constraints II) 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 3382 (Maximum Area Rectangle With Point Constraints II)?

The time complexity for LeetCode 3382 (Maximum Area Rectangle With Point Constraints II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3382 on DevExCode?

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

Back to LeetCode Solutions