LeetCode – Minimum Area Rectangle (Java)

Given a set of points in the x and y axes, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.

Analysis

In the first impression, this problem may be solved by 3 different approaches:

First, the max value on the grid is a constant, 40000. Maybe we can iterate over all possibilities of the grip. By calculating the total number of operations, this is not good. Its time is at list O(N^4).
Second, maybe we can use a DFS method to search each point and see if a rectangle can be formed from each point. This may work, but this may be a simpler solution.
Third, by looking at the grid, we can think about what is the requirement of a rectangle. We can see that if there is a rectangle, there should be 2 points for the diagonal (x1, y1) and (x2, y2). There should also be two other points corresponding to the two diagonal points: (x1, y2) and (x2, y1). So the solution naturally come out. We iterate over all possibilities of two diagonal points and see if the other two points exist.

So we go with the third solution. We can use a hash map to make point searching constant time. The time complexity of the solution below is O(N^2).

Java Solution

public int minAreaRect(int[][] points) {
    if(points==null || points.length==0){
        return 0;
    }
 
    Arrays.sort(points, new Comparator<int[]>(){
        public int compare(int[] a, int[] b){
            if(a[0]!=b[0]){
                return Integer.compare(a[0], b[0]);
            }else{
                return Integer.compare(a[1], b[1]);
            }
        } 
    });
 
    HashMap<Integer, HashSet<Integer>> xMap = new HashMap<>();
    HashMap<Integer, HashSet<Integer>> yMap = new HashMap<>();
 
    for(int i=0; i<points.length; i++){
        int x = points[i][0];
        int y = points[i][1];
 
        //x map
        HashSet<Integer> setX = xMap.get(x);
 
        if(setX==null){
            setX = new HashSet<Integer>();  
            xMap.put(x, setX);
        }
 
        setX.add(y);
 
        //y map
        HashSet<Integer> setY = yMap.get(y);
 
        if(setY==null){
            setY = new HashSet<Integer>();  
            yMap.put(y, setY);
        }
 
        setY.add(x);
    }
 
    int result = Integer.MAX_VALUE;
 
    for(int i=0; i<points.length-1; i++){
        for(int j=i+1; j<points.length; j++){
            int x1 = points[i][0];
            int y1 = points[i][1];
            int x2 = points[j][0];
            int y2 = points[j][1];
 
            if(xMap.get(x1).contains(y2) && yMap.get(y1).contains(x2)){
                int area = Math.abs((x1-x2)*(y1-y2));
                if(area>0){
                    result = Math.min(result, area);
                }
            }
        }
    }
 
    if(result == Integer.MAX_VALUE){
        return 0;
    }
 
    return result;
}

10 thoughts on “LeetCode – Minimum Area Rectangle (Java)”

  1. My god… Hahah.. anyway I hope she doesn’t mistake that heewon was the person there trying to forcefully reveal her face their… Yk I don’t think she’ll ever do something like that without reason… It’s more like I respect heewon rather than saying I like her

  2. If I have points:
    (0,1)
    (1,0)
    (0,-1)
    (-1,0)

    What is the minimum area of a rectangle formed from these points? They make a square tilted by 45 degrees. My assumption was that they “form” a rectangle of area 4, as that is the smallest rectangle that can cover these points, but looking at the answer it seems that the question means something else – the smallest rectangle that can be constructed with 4 of the points in the set as corners.

    It is possible that I am just bad at reading questions, but more likely is that the question is poorly stated. I should not have to look at the answer to determine the meaning of the question. Regarding the other remarks complimenting your question – they feel like fluff. This is a bad question. At the very least provide an example to illustrate the question if you find it difficult to write a well defined question.

Leave a Comment