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. java⑩

    1.for循环: for循环语法 for(表达式1;表达式2;表达式3){ 循环体4} 表达式1:初始化变量 只执行一次!表达式2:循环条件 满足条件进入循环体4表达式3:迭代变量 如果循环体 中只有 ...

  2. 《Python》hashlib模块、configparser模块、logging模块

    一.hashlib模块 Python的hashlib模块中提供了常见的摘要算法,如md5,sha1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的字符串(通 ...

  3. python 最小二乘拟合,反卷积,卡方检验

    import numpy as np # from enthought.mayavi import mlab ''' ogrid[-1:5:6j,-1:5:6j] [array([[-1. ], [ ...

  4. flask小例

    #写一个app.py,处理3个URL: ''' GET / : 首页,返回Home; GET /signin:登录页,显示登录表单; POST /signin: 处理登录表单,显示登录结果. ''' ...

  5. [Leetcode 881]船救人 Boats to Save People 贪心

    [题目] The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each b ...

  6. Linux学习 :多线程编程

    1.Linux进程与线程() 进程:通过fork创建子进程与创建线程之间是有区别的:fork创建出该进程的一份拷贝,创建时额外申请了新的内存空间以及存储代码段.数据段.BSS段.堆.栈空间,     ...

  7. 3.9 C++多继承

    参考:http://www.weixueyuan.net/view/6366.html 总结: C++中一个派生类中允许有两个及以上的基类,我们称这种情况为多继承 使用多继承可以描述事物之间的组合关系 ...

  8. python字符串转换成数字

    Action(){ int i; char *s="{str}"; i=atoi(lr_eval_string(s)); lr_output_message("%d&qu ...

  9. Oracle表的操作

    --创建表 CREATE [GLOBAL TEMPORARY] TABLE table_name( coloum_name TYPE [CONSTRAINT constraint_def DEFAUL ...

  10. <HBase><读写><LSM>

    Overview HBase中的一个big table,首先会按行划分成一些region(这些region之间是有序的,由startkey保证),每个region分配到不同的节点进行存储.因此,reg ...