LeetCode 939. Minimum Area Rectangle (最小面积矩形)
题目标签: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;
}
}
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 939. Minimum Area Rectangle (最小面积矩形)的更多相关文章
- 【leetcode】939. Minimum Area Rectangle
题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from t ...
- 【LeetCode】939. Minimum Area Rectangle 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 确定对角线,找另外两点(4sum) 字典保存出现的x ...
- UVA10173 Smallest Bounding Rectangle 最小面积矩形覆盖
\(\color{#0066ff}{题目描述}\) 给定n(>0)二维点的笛卡尔坐标,编写一个程序,计算其最小边界矩形的面积(包含所有给定点的最小矩形). 输入文件可以包含多个测试样例.每个测试 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- [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 ...
- 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...
- LeetCode939 最小面积矩形
LeetCode939最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. Input [[1,1],[ ...
- 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 ...
- 963. Minimum Area Rectangle II
Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...
随机推荐
- 什么 是JUnit?
JUnit详解: http://www.cnblogs.com/eggbucket/archive/2012/02/02/2335697.html
- LDA算法(入门篇)
一. LDA算法概述: 线性判别式分析(Linear Discriminant Analysis, LDA),也叫做Fisher线性判别(Fisher Linear Discriminant ,FLD ...
- node遍历给定目录下特定文件,内容合并到一个文件
遍历目录用了fs.readdir这个异步方法,得到当前目录下所有的文件和目录的一个数组.然后判断: if文件,并且后缀符合设定的规则(本文例子是符合后缀ts,js)直接用同步方法写入, if目录,继续 ...
- Maven 项目debug调试时报Source not found.异常
正如异常描述,那么解决方法当然是指定源码. 测试于:Maven 3.0.5, eclipse-jee-indigo-SR2-win32 异常信息: Source not found. 解决方法: 首先 ...
- MFC 课程总结
<基于MFC框架开发>马志国 1491989781 MFC课程的组成 1.1 MFC应用程序的组成部分.执行机制和执行流程(10.5天). 1.2 Windows平台上的数据库访问技术(1 ...
- 数据结构---二叉搜索树BST实现
1. 二叉查找树 二叉查找树(Binary Search Tree),也称为二叉搜索树.有序二叉树(ordered binary tree)或排序二叉树(sorted binary tree),是指一 ...
- cookie的原理
一般来说,Cookie通过HTTP Headers从服务器端返回到浏览器上.首先,服务器端在响应中利用Set-Cookie header来创建一个Cookie ,然后,浏览器在它的请求中通过Cooki ...
- MySql-了解存储引擎
怎么应对不同版本 在不同的 mysql 版本中,很多特性和语法有可能是不一样的,我们怎么样才能知道当前版本的语法是什么样呢?最好的办法是学会使用 mysql 的帮助. A.按照层次看帮助 例如:mys ...
- APUE 文件和目录
文件和目录 Unix 所有的文件都对应一个 struct stat,包含了一个文件所有的信息. #include <sys/stat.h> struct stat { mode_t st_ ...
- Ubuntu notes
ubuntu notes Table of Contents 1. backup data 2. Basics Ubuntu 3. Install, uninstall packages 4. Bas ...