题目标签: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. Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1

    记录一次傻逼的问题, 自己把自己蠢哭:Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1 错误说明: 返回的1是影响的行数,并不是自增的主键id: 想要获取自增主键id,需要 ...

  2. Django基础之admin功能

    Django默认开起了后台 1.访问admin后台 2.用户和密码进行登录 ============================================================== ...

  3. JDBC更新10W级以上数据性能优化

    随笔缘由: 系统完成到一定程度,少不了要往数据库中添加大量数据进行性能测试. 我用程序做数据10W条,使用jdbc批更新的API,发现每次只能插入2W多条记录. 一番小小研究,觉得总结一下可能有些意义 ...

  4. CherryPy 入门

    CherryPy是一个Python的HTTP框架,可以用Python来处理HTTP请求然后返回结果. 1. 安装 可以去这个地址下载 CherryPy-3.1.2.win32.exe .或者去这个链接 ...

  5. (转) 淘淘商城系列——使用SolrJ查询索引库

    http://blog.csdn.net/yerenyuan_pku/article/details/72908538 我们有必要在工程中写查询索引库的代码前先进行必要的测试.我们先到Solr服务页面 ...

  6. 安卓app测试之Monkey测试

    一.Monkey特点 1.运行时机:一般是产品稳定后 首轮功能测试完成的夜间进行 2.需要知道packageName 3.目的:主要测试产品是否存在崩溃问题和ANR问题. 二.获取包名的两个方法 首先 ...

  7. HDU多校Round 8

    Solved:2 rank:141 D. Parentheses Matrix n,m有一个小于6的时候是一种构造方法 答案是n + (m - 2) / 2 (n > m) 都大于6的时候 可以 ...

  8. anaconda镜像

    下载一个包老是下载不下来,于是放弃了官方版改为国内镜像. 清华镜像 (打开Anaconda Prompt   或者 打开cmd,运行下面的命令) conda config --add channels ...

  9. Nginx+Tomcat简单负载均衡

    Nginx,Apache安装完成 复制Tomcat:    tomcat-8080    tomcat-8081 启动Tomcat8080: cd /usr/local/tomcat-8080/bin ...

  10. 计算机网络篇(前端、HTTP)

    全端工程师需知道的计算机网络知识 一.网络篇-http报文详解 1. 分类 请求报文 响应报文 2. 报文结构 (一).请求报文 一个HTTP请求报文由请求行(request line).请求头部(h ...