题目标签:HashMap

  题目给了我们一组 xy 上的点坐标,让我们找出 能组成矩形里最小面积的那个。

  首先遍历所有的点,把x 坐标当作key 存入map, 把重复的y坐标 组成set,当作value 存入map。

  然后遍历所有的点,找出 对角的两个点, 再去map里确认是否存在剩下的两个对角点,计算面积,一直保留最小的那个面积。

Java Solution:

Runtime: 214 ms, faster than 78.80%

Memory Usage: 61.5 MB, less than 5.97%

完成日期:03/27/2019

关键点:找对角2个点,方便判断和操作。

class Solution {
public int minAreaRect(int[][] points) {
Map<Integer, Set<Integer>> map = new HashMap<>();
int minArea = Integer.MAX_VALUE; // save each point into map by key as x, value as multiple y
for(int[] point : points)
{
if(!map.containsKey(point[0]))
map.put(point[0], new HashSet<>()); map.get(point[0]).add(point[1]);
} // find 2 diagonal points and then find the other 2 diagonal points to calculate the area
for(int[] point1 : points)
{
for(int[] point2: points)
{
if(point1[0] == point2[0] || point1[1] == point2[1]) // if point1 and point2 are not diagonal
continue; if(map.get(point1[0]).contains(point2[1]) && map.get(point2[0]).contains(point1[1])) // if find the other 2 diagonal points
minArea = Math.min(minArea, Math.abs(point2[0] - point1[0]) * Math.abs(point2[1] - point1[1]));
}
} return minArea == Integer.MAX_VALUE ? 0 : minArea;
}
}

参考资料:https://leetcode.com/problems/minimum-area-rectangle/discuss/?currentPage=1&orderBy=recent_activity&query=

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 939. Minimum Area Rectangle (最小面积矩形)的更多相关文章

  1. 【leetcode】939. Minimum Area Rectangle

    题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from t ...

  2. 【LeetCode】939. Minimum Area Rectangle 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 确定对角线,找另外两点(4sum) 字典保存出现的x ...

  3. UVA10173 Smallest Bounding Rectangle 最小面积矩形覆盖

    \(\color{#0066ff}{题目描述}\) 给定n(>0)二维点的笛卡尔坐标,编写一个程序,计算其最小边界矩形的面积(包含所有给定点的最小矩形). 输入文件可以包含多个测试样例.每个测试 ...

  4. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  5. [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 ...

  6. 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...

  7. LeetCode939 最小面积矩形

    LeetCode939最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. Input [[1,1],[ ...

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

  9. 963. Minimum Area Rectangle II

    Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...

随机推荐

  1. NX自动出图 发布啦

    经过4个月的努力,终于面世啦!!!!1.安装文件 :http://yunpan.cn/Q49TWSJmy2i5Z    请下载后,按照“安装说明.txt ”进行安装!2.学习视频:http://yun ...

  2. Vue实战之插件 sweetalert 的使用

    安装npm install sweetalert2@7.15.1 --save 封装 sweetalertimport swal from 'sweetalert2' export default { ...

  3. SQlite数据库框架:LitePal

    常用的数据库框架Android的发展的速度是难以置信的,Android出来哪一年我还在小学上学很,还能很清楚的记得,那年一切,但是那个时候的我怎么可能也不会想到自己将来会要去做Android.Andr ...

  4. RabbitMQ系列(七)--批量消息和延时消息

    批量消息发送模式 批量消息是指把消息放到一个集合统一进行提交,这种方案设计思路是希望消息在一个会话里,比如放到threadlocal里的集合,拥有相同 的会话ID,带有这次提交信息的size等属性,最 ...

  5. 2019西安多校联训 Day1

    试题链接:http://www.accoders.com/contest.php?cid=1893  考试密码请私信;    T1 明明就是O(n)的模拟,强行打成二分QAQ 思路:判断收尾是否为1或 ...

  6. 简单的jsonp实现跨域原理

    什么原因使jsonp诞生?  传说,浏览器有一个很重要的安全限制,叫做"同源策略".同源是指,域名,协议,端口相同.举个例子,用一个浏览器分别打开了百度和谷歌页面,百度页面在执行脚 ...

  7. 在mac上面运行cherrytree

    下载源码包 wget http://www.giuspen.com/software/cherrytree-0.38.4.tar.xz 解压 tar -xvf cherrytree-0.38.4.ta ...

  8. 根据屏幕的大小改变rem的参考值

    移动端一半会选用rem+flex布局的方式,下面是根据屏幕的宽度,动态的改变rem的参考值 var screenWidth;             var html = document.getEl ...

  9. TensorFlow — 相关 API

    TensorFlow — 相关 API TensorFlow 相关函数理解 任务时间:时间未知 tf.truncated_normal truncated_normal( shape, mean=0. ...

  10. phpcms 搭建宣传网站首页

    1 .修改后台提交的表单信息展示: 文件路径: phpcms\modules\formguide\template\formguide_info_list.tpl.php function getQu ...