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.


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){
                return Integer.compare(a[0], b[0]);
                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);
            setX = new HashSet<Integer>();  
            xMap.put(x, setX);
        //y map
        HashSet<Integer> setY = yMap.get(y);
            setY = new HashSet<Integer>();  
            yMap.put(y, setY);
    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));
                    result = Math.min(result, area);
    if(result == Integer.MAX_VALUE){
        return 0;
    return result;
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Plupsy

    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

  • George Onetimer

    If I have points:

    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.

  • Hey,
    Thanks for sharing this.
    Learn free Programming Languages

  • 9 Most Popular JAVA Frameworks Helpful in Java App Development

  • Thanks for Sharing LeetCode – Minimum Area Rectangle (Java).
    Go- http://aptronnoida.in/best-java-training-in-noida.html

  • Hi ,
    thanks for sharing, its helpul.

  • Great post . tamilnadu textbooks

  • Hi,

    Thank for sharing such a nice post on your blog keep it up and share more.

    Free Crack Software Download

  • Amlida


    Thank for sharing such a nice information on your blog keep it up and share more related to java LeetCode – Minimum Area Rectangle. home network security device