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】963. Minimum Area Rectangle II
		题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ... 
- 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ... 
- [Swift]LeetCode963. 最小面积矩形 II | Minimum Area Rectangle II
		Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ... 
- 963. Minimum Area Rectangle II
		Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ... 
- LC 963. Minimum Area Rectangle II
		Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ... 
- 【LeetCode】939. Minimum Area Rectangle 解题报告(Python & C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 确定对角线,找另外两点(4sum) 字典保存出现的x ... 
- 【leetcode】939. Minimum Area Rectangle
		题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from t ... 
- [Swift]LeetCode939. 最小面积矩形 | Minimum Area Rectangle
		Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these p ... 
- LeetCode 939. Minimum Area Rectangle (最小面积矩形)
		题目标签:HashMap 题目给了我们一组 xy 上的点坐标,让我们找出 能组成矩形里最小面积的那个. 首先遍历所有的点,把x 坐标当作key 存入map, 把重复的y坐标 组成set,当作value ... 
随机推荐
- 使用markdown第一个博客
			markdown,I coming ! System.out.println("I m coming"); 
- jvm常用参数
			-Xms512m:初始堆内存 -Xmx512m:最大堆内存 -XX:PermSize=256m:初始永久代内存(方法区,非堆) -XX:MaxPermSize=256m:最大永久代内存(方法区,非堆) ... 
- day34 线程池 协程
			今日内容: 1. 线程的其他方法 2.线程队列(重点) 3.线程池(重点) 4.协程 1.线程的其他方法 语法: Threading.current_thread() # 当前正在运行的线程对象的一个 ... 
- 使用perfect进行服务端开发
			最近闲来无事,研究了下基于perfect的swift后端开发.根据大神的博客进行了简单的配置,加深下印象也算是和各位分享一下. 参考博客:http://www.cnblogs.com/ludashi ... 
- (C/C++学习笔记) 一. 基础知识
			一. 基础知识 ● 程序和C/C++ 程序: 根据Wirth (1976), Algorithms + Data Structures = Programs. Whence C: 1972, Denn ... 
- 从R-CNN到FAST-RCNN再到Faster R-CNN
			(Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks) R-CNN: (1)输入测试图像: ... 
- centos7配置hadoop集群
			一:测试环境搭建规划: 主机名称 IP 用户 HDFS YARN hadoop11 192.168.1.101 hadoop NameNode,DataNode NodeManager hadoop1 ... 
- 2017   秦皇岛CCPC Balloon Robot (ZOJ  3981)
			题意:给出n个队伍,m个座位,q次A题的队伍与时间,下一行是n个队伍的坐的位置,再下面q行就是第x个队再第y秒A出一道题,然后有一个机器人,开始位置由你选,他每走一步 他就会向右走一格,走到m的时候会 ... 
- ACID/CAP/BASE 理论知识
			ACID是事务的四大特性,想要成为事务,必须具备这四点. Atomicity 原子性体现在对于一个事务来讲,要么一起执行成功要么一起失败,执行的过程中是不能被打断或者执行其他操作的. Consiste ... 
- day 46 前端基础 基本框架
			注意一点 使用绝对路径的时候 在pxm里 打开显示不了图片 可以直接找到那个实际的网页去打开 还可能是图片的格式尽量用jpg一 详细解释 <!DOCTYPE html>声明为HTML5文档 ... 
