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. 一个canvas的demo

    该demo放于tomcat下运行,否则出现跨域错误 <!DOCTYPE html> <html> <head> <meta charset="utf ...

  2. 【转载】 unity 塔防游戏

    原文地址: Part1的地址:http://www.cnblogs.com/lcxBlog/p/6075984.html Part2的地址:http://www.cnblogs.com/lcxBlog ...

  3. python全栈开发笔记---基本数据类型--字符串魔法

    字符串: def capitalize(self, *args, **kwargs) test = "aLxs" v = test.capitalize() #capitalize ...

  4. MATLAB中mexFunction函数的接口规范(转)

    reference:https://www.cnblogs.com/haoyul/p/5606720.html https://www.cnblogs.com/zeakey/p/3984733.htm ...

  5. 记录搭建ssm项目

    搞java也快3年了,搭建一个ssm居然有点吃力. 参考链接:https://blog.csdn.net/gebitan505/article/details/44455235/ 环境准备:jdk8. ...

  6. mysql修改lower_case_table_names产生的问题

    1.参数含义: lower_case_table_names: 此参数不可以动态修改,必须重启数据库 lower_case_table_names = 1 表名存储在磁盘是小写的,但是比较的时候是不区 ...

  7. 牛客多校第五场 F take

    链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 题目描述 Kanade has n boxes , the i-th box has p[i] ...

  8. vue-router + ElementUI实现NavMenu 导航菜单 选中状态的切换

    elemen-ui官方网站:http://element.eleme.io/#/zh-CN/component/menu 新手小白利用vue+element-ui尝试搭建后台管理系统, 效果是这样的, ...

  9. webpack对多个模块依赖进行打包

    [ webpack3.0.0刚刚出来  所以文章是跟着低版本 教程 操作熟悉  结果好多对不上喔] 七:webpack对多个模块依赖进行打包 通过一刚开始我们了解到 webpack支持commonJS ...

  10. oracle语句录

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