Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes.

If there isn't any rectangle, return 0.

Example 1:

Input: [[1,2],[2,1],[1,0],[0,1]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.

Example 2:

Input: [[0,1],[2,1],[1,1],[1,0],[2,0]]
Output: 1.00000
Explanation: The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.

Example 3:

Input: [[0,3],[1,2],[3,1],[1,3],[2,1]]
Output: 0
Explanation: There is no possible rectangle to form from these points.

Example 4:

Input: [[3,1],[1,1],[0,1],[2,1],[3,3],[3,2],[0,2],[2,3]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [2,1],[2,3],[3,3],[3,1], with an area of 2.

Note:

  1. 1 <= points.length <= 50
  2. 0 <= points[i][0] <= 40000
  3. 0 <= points[i][1] <= 40000
  4. All points are distinct.
  5. Answers within 10^-5 of the actual value will be accepted as correct.

Approach #1: Math. [Java]

class Solution {
public double minAreaFreeRect(int[][] points) {
int len = points.length;
if (len < 4) return 0.0;
double ret = Double.MAX_VALUE;
Map<String, List<int[]>> map = new HashMap<>();
for (int i = 0; i < len; ++i) {
for (int j = i+1; j < len; ++j) {
long diagonal = (points[i][0] - points[j][0]) * (points[i][0] - points[j][0]) +
(points[i][1] - points[j][1]) * (points[i][1] - points[j][1]);
double centerX = (double)(points[i][0] + points[j][0]) / 2;
double centerY = (double)(points[i][1] + points[j][1]) / 2;
String key = "" + diagonal + "+" + centerX + "+" + centerY;
if (map.get(key) == null) map.put(key, new ArrayList<int[]>());
map.get(key).add(new int[]{i, j});
}
} for (String key : map.keySet()) {
List<int[]> list = map.get(key);
if (list.size() < 2) continue;
for (int i = 0; i < list.size(); ++i) {
for (int j = i+1; j < list.size(); ++j) {
int p1 = list.get(i)[0];
int p2 = list.get(j)[0];
int p3 = list.get(j)[1];
double x = Math.sqrt((points[p1][0] - points[p2][0]) * (points[p1][0] - points[p2][0])
+ (points[p1][1] - points[p2][1]) * (points[p1][1] - points[p2][1]));
double y = Math.sqrt((points[p1][0] - points[p3][0]) * (points[p1][0] - points[p3][0])
+ (points[p1][1] - points[p3][1]) * (points[p1][1] - points[p3][1]));
double area = x * y;
ret = Math.min(ret, area);
}
}
} return ret == Double.MAX_VALUE ? 0.0 : ret;
}
}

  

Analysis:

1. Two diagonals of a rectangle bisect each other, and are of equal length.

2. The map's key is String including diagonal length and coordinate of the diagonal center; map's vlaue is the index of two points forming the diagonal.

Reference:

https://leetcode.com/problems/minimum-area-rectangle-ii/discuss/208361/JAVA-O(n2)-using-Map

963. Minimum Area Rectangle II的更多相关文章

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

  2. 【leetcode】963. Minimum Area Rectangle II

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

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

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

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

  5. Leetcode963. Minimum Area Rectangle II最小面积矩形2

    给定在 xy 平面上的一组点,确定由这些点组成的任何矩形的最小面积,其中矩形的边不一定平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. 示例 1: 输入:[[1,2],[2,1],[1,0] ...

  6. 计算几何-Minimum Area Rectangle II

    2020-02-10 21:02:13 问题描述: 问题求解: 本题由于可以暴力求解,所以不是特别难,主要是用来熟悉计算几何的一些知识点的. public double minAreaFreeRect ...

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

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

  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 - Minimum Area Rectangle

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

随机推荐

  1. 医学图像 | DualGAN与儿科超声心动图分割 | MICCAI

    文章转自微信公众号:「机器学习炼丹术」 作者:炼丹兄(已授权) 联系方式:微信cyx645016617(欢迎交流共同进步) 论文名称:"Dual Network Generative Adv ...

  2. win10使用cmd命令关闭防火墙

    在搜索框内输入cmd,右键选择管理员运行 然后输入: NetSh Advfirewall set allprofiles state off #关闭防火墙 Netsh Advfirewall show ...

  3. Golang+chromedp+goquery 简单爬取动态数据

    目录 Golang+chromedp+goquery 简单爬取动态数据 Golang的安装 下载golang软件 解压golang 配置golang 重新导入配置 chromedp框架的使用 实际的代 ...

  4. rsyslog日志总结

    rsyslog日志总结 一 rsyslog介绍 syslogd被rsyslog取代 将日志写入数据库 可以利用模块和插件控制输入输出 rsyslog程序管理本地和远程日志 安装软件 根据需求修改配置文 ...

  5. allure报告详解+jenkins配置

    今天的博客分为两部分 1.allure报告实战 2.allure结合jenkins 一.allure 1.allure安装 a.下载路径 https://repo.maven.apache.org/m ...

  6. Shell:如何写一个多选菜单的脚本

    Blog:博客园 个人 翻译自How to Create a Multiple Choice Menu in Bash Scripts 目录 多选菜单脚本介绍 配置输入提示 创建预定选项列表 创建预选 ...

  7. python-实现顺序栈

    7 class sqstack(object): 8 def __init__(self,maxsize): 9 self.maxsize = maxsize 10 self.stackElem = ...

  8. python常见错误和异常

    1.BaseExeception 所有异常的基类 2.SystemEixt 解释器请求退出 3.KeyboardInterrupt 用户中断执行 4.Exception 常规错误的基类 5.StopI ...

  9. PTA 输出数组元素

    7-3 输出数组元素 (15 分)   本题要求编写程序,对顺序读入的n个整数,顺次计算后项减前项之差,并按每行三个元素的格式输出结果. 输入格式: 输入的第一行给出正整数n(1).随后一行给出n个整 ...

  10. 14、MyBatis教程之全部(包括所有章节)

    MyBatis 3.5.5 教程 1.环境准备 jdk 8 + MySQL 5.7.19 maven-3.6.1 IDEA 学习前需要掌握: JDBC MySQL Java 基础 Maven Juni ...