# 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!=b){ return Integer.compare(a, b); }else{ return Integer.compare(a, b); } } });   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]; int y = points[i];   //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]; int y1 = points[i]; int x2 = points[j]; int y2 = points[j];   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; }```
Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• George Onetimer

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.

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

• 9 Most Popular JAVA Frameworks Helpful in Java App Development
https://cutt.ly/ak9LbtJ

• 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.