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的更多相关文章

  1. 【leetcode】963. Minimum Area Rectangle II

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

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

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

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

  4. 963. Minimum Area Rectangle II

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

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

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

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

  7. 【leetcode】939. Minimum Area Rectangle

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

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

  9. LeetCode 939. Minimum Area Rectangle (最小面积矩形)

    题目标签:HashMap 题目给了我们一组 xy 上的点坐标,让我们找出 能组成矩形里最小面积的那个. 首先遍历所有的点,把x 坐标当作key 存入map, 把重复的y坐标 组成set,当作value ...

随机推荐

  1. PHP socket以及http、socket、tcp、udp

    一.TCP/UDP/Socket TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,是一个工业标准的协议集,它是为 ...

  2. 《图解HTTP》读书笔记(转)

    reference:https://www.cnblogs.com/edisonchou/p/6013450.html   目前国内讲解HTTP协议的书是在太少了,记忆中有两本被誉为经典的书<H ...

  3. DevExpress ASP.NET Bootstrap Controls v18.2新功能详解(二)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Boot ...

  4. nio的简单学习

    参考链接: http://www.iteye.com/magazines/132-Java-NIO https://www.cnblogs.com/xiaoxi/p/6576588.html http ...

  5. debug fortran

    exmple: gfortran -g -fcheck=all -Wall segf.f90

  6. AntPathMatcher做路径匹配

    转发自: http://www.cnblogs.com/leftthen/p/5212221.html 需要看详细的请看上面的链接 这里以我这里的一个Filter 中需要对路径做例外处理,filter ...

  7. 51nod1009

    给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数.   例如:n = 12,包含了5个1.1,10,12共包含3个1,11包含2个1,总共5个1. Input 输入N( ...

  8. oracle语句录

    从表中选出一个某个单位最近的记录 select * from RSDL_SHXX where sbsj in (select max (sbsj) from RSDL_SHXX where DW_ID ...

  9. C代码与C++代码之间的相互调用

    1.showCpp.cpp #include <iostream> using namespace std; // 声明为C语言可以调用的函数 extern "C" v ...

  10. python3 自学第一天,python 介绍

    1.python的介绍: 是一个无聊的人创造的 2.python的格式: 跟java这些语言格式不一样用的是缩进来编码(区块) 一般是四个空格,这样更简洁 3.编码格式: python3跟python ...