LeetCode 963: Minimum Area Rectangle II Solution

Master LeetCode problem 963 (Minimum Area Rectangle II), 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.

963. Minimum Area Rectangle II

Problem Explanation

Explanation:

To solve this problem, we can iterate over all pairs of points and consider them as the diagonal of a potential rectangle. The other two points of the rectangle must have the same distance from the midpoint of the diagonal. We can calculate the midpoint and distance between points easily using simple Euclidean distance formulas.

For each pair of points as diagonals, we need to consider all other points and check if they form a rectangle with the given diagonal. We can use a set to store the points and their distances from the midpoint. If we find a pair of points with the same distance, we can calculate the area of the rectangle formed by these points and update the minimum area if necessary.

Finally, we return the minimum area found or 0 if no valid rectangle can be formed.

Time Complexity: O(n^2) where n is the number of points. We iterate over all pairs of points and for each pair, we iterate over all other points. Space Complexity: O(n) for storing the distances in the set.

:

Solution Code

class Solution {
    public double minAreaFreeRect(int[][] points) {
        double minArea = Double.MAX_VALUE;
        Set<String> pointSet = new HashSet<>();
        
        for (int[] p : points) {
            pointSet.add(p[0] + "," + p[1]);
        }
        
        for (int i = 0; i < points.length; i++) {
            for (int j = i + 1; j < points.length; j++) {
                int[] p1 = points[i];
                int[] p2 = points[j];
                
                for (int k = j + 1; k < points.length; k++) {
                    int[] p3 = points[k];
                    
                    int x = p1[0] + p2[0] - p3[0];
                    int y = p1[1] + p2[1] - p3[1];
                    
                    if (pointSet.contains(x + "," + y)) {
                        double area = Math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])) *
                                      Math.sqrt((p1[0] - x) * (p1[0] - x) + (p1[1] - y) * (p1[1] - y));
                        minArea = Math.min(minArea, area);
                    }
                }
            }
        }
        
        return minArea == Double.MAX_VALUE ? 0 : minArea;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 963 (Minimum Area Rectangle II)?

This page provides optimized solutions for LeetCode problem 963 (Minimum Area Rectangle 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 963 (Minimum Area Rectangle II)?

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

Can I run code for LeetCode 963 on DevExCode?

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

Back to LeetCode Solutions