zoukankan      html  css  js  c++  java
  • [LC] 939. 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
    
    class Solution {
        public int minAreaRect(int[][] points) {
            int min = Integer.MAX_VALUE;
            Map<Integer, Set<Integer>> map = new HashMap<>();
            // find all x and their associated y
            for (int[] point : points) {
                if (!map.containsKey(point[0])) {
                    map.put(point[0], new HashSet<>());
                }
                map.get(point[0]).add(point[1]);
            }
            
            for (int[] p1 : points) {
                for (int[] p2 : points) {
                    if (p1[0] != p2[0] && p1[1] != p2[1]) {
                        // make sure diagnal points p1, p2 having the connecting p3 and p4
                        if (map.get(p1[0]).contains(p2[1]) && map.get(p2[0]).contains(p1[1])) {
                            int curArea = Math.abs((p1[0] - p2[0]) * (p1[1] - p2[1]));
                            min = Math.min(min, curArea);
                        }
                    }
                }
            }
            return min == Integer.MAX_VALUE ? 0: min;
        }
    }
  • 相关阅读:
    十进制转任意进制
    整型与字符串转换
    最长递增子序列(动态规划)
    睡眠理发师问题
    区间最值问题(RMQ)
    分解质因子
    数字统计
    After all, tomorrow is another day.
    【USB电平】电脑USB电平
    【有源滤波】滤波基础知识
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12485402.html
Copyright © 2011-2022 走看看