LeetCode - Minimum Area Rectangle

Posted IncredibleThings

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode - Minimum Area Rectangle相关的知识,希望对你有一定的参考价值。

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

If there isn‘t any rectangle, return 0.

 

Example 1:

Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:

Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
 

Note:

1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct.

核心思想是利用对角线的原理定下两个点,根据对角线在找到另外两个点

如何把一个array pair存到hash里,第一次我用了int -> string, 超时了

class Solution {
    public int minAreaRect(int[][] points) {
        if(points == null || points.length == 0 || points[0].length ==0){
            return -1;
        }
        Set<String> set = new HashSet<>();
        for(int[] point : points){
            set.add(Integer.toString(point[0])+":"+Integer.toString(point[1]));
        }
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < points.length-1; i++){
            for(int j = i+1; j < points.length; j++){
                int [] pointA = points[i];
                int [] pointB = points[j];
                if(pointA[0] != pointB[0] && pointA[1] != pointB[1]){
                    if(set.contains(Integer.toString(pointA[0])+":"+Integer.toString(pointB[1])) && set.contains(Integer.toString(pointB[0])+":"+Integer.toString(pointA[1]))){
                        int cur = Math.abs((pointB[0] - pointA[0]) * (pointB[1] - pointA[1]));
                        if(cur < min){
                            min = cur;
                        }
                    }
                }
            }
        }
        if(min == Integer.MAX_VALUE){
            return 0;
        }
        return min;
    }
}

第二次看了别人的代码, 原来可以按照把每一个pair的x存成key,y轴存成一个hashset。

class Solution {
    public int minAreaRect(int[][] points) {
        if(points == null || points.length == 0 || points[0].length ==0){
            return 0;
        }
        Map<Integer, Set<Integer>> map = new HashMap<>();
        for(int[] point : points){
            if(map.containsKey(point[0])){
                map.get(point[0]).add(point[1]);
            }
            else{
                Set<Integer> set = new HashSet<Integer>();
                set.add(point[1]);
                map.put(point[0], set);
            }
        }
        
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < points.length-1; i++){
            for(int j = i+1; j < points.length; j++){
                int [] pointA = points[i];
                int [] pointB = points[j];
                if(pointA[0] != pointB[0] && pointA[1] != pointB[1]){
                    if(map.get(pointA[0]).contains(pointB[1]) && map.get(pointB[0]).contains(pointA[1])){
                        int cur = Math.abs((pointB[0] - pointA[0]) * (pointB[1] - pointA[1]));
                        if(cur < min){
                            min = cur;
                        }
                    }
                }
            }
        }
        if(min == Integer.MAX_VALUE){
            return 0;
        }
        return min;
    }
}

 

以上是关于LeetCode - Minimum Area Rectangle的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode - Minimum Area Rectangle

leetcode939. Minimum Area Rectangle

LeetCode 153. Find Minimum in Rotated Sorted Array

leetcode-Find Minimum in Rotated Sorted Array-153

LC 963. Minimum Area Rectangle II

leetcode1200. Minimum Absolute Difference