963. Minimum Area Rectangle II
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 <= points.length <= 50
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
- All points are distinct.
- 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的更多相关文章
- 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 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...
- [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 ...
- Leetcode963. Minimum Area Rectangle II最小面积矩形2
给定在 xy 平面上的一组点,确定由这些点组成的任何矩形的最小面积,其中矩形的边不一定平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. 示例 1: 输入:[[1,2],[2,1],[1,0] ...
- 计算几何-Minimum Area Rectangle II
2020-02-10 21:02:13 问题描述: 问题求解: 本题由于可以暴力求解,所以不是特别难,主要是用来熟悉计算几何的一些知识点的. public double minAreaFreeRect ...
- 【LeetCode】939. Minimum Area Rectangle 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 确定对角线,找另外两点(4sum) 字典保存出现的x ...
- [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 ...
- LeetCode - Minimum Area Rectangle
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these p ...
随机推荐
- css3自动换行排列
如果一行放不下就会自动换行 display: flex; flex-wrap: wrap; 示例 : html <div class="container"> < ...
- Windows 环境下搭建 RocketMQ
Apache 官网: http://rocketmq.apache.org/ RocketMQ 的 Github 地址: English:https://github.com/apache/rocke ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- 从零搭建一个IdentityServer——单页应用身份验证
上一篇文章我们介绍了Asp.net core中身份验证的相关内容,并通过下图描述了身份验证及授权的流程: 注:改流程图进行过修改,第三方用户名密码登陆后并不是直接获得code/id_token/acc ...
- python flask框架详解
Flask是一个Python编写的Web 微框架,让我们可以使用Python语言快速实现一个网站或Web服务.本文参考自Flask官方文档, 英文不好的同学也可以参考中文文档 1.安装flask pi ...
- CVE-2017-7504-JBoss JMXInvokerServlet 反序列化
漏洞分析 https://paper.seebug.org/312/ 漏洞原理 这是经典的JBoss反序列化漏洞,JBoss在/invoker/JMXInvokerServlet请求中读取了用户传入的 ...
- Spring 的 IOC
1. 什么是IOC IOC的好处 IOC的思想是将需要的对象通过外部传入进来,而不是自己创建.这样的设计方式更加灵活.在Spring中对象之间的依赖关系也是由IOC容器来维护(类与类之间的依赖关系,使 ...
- Codeforces Round #684 (Div. 2)
A 讨论三种情况,不换/全换成0/全换成1 ,取一个花费最小值 #include <bits/stdc++.h> using namespace std; const int N = 10 ...
- java实现所有排序算法
package sort;public class Sort { public static void BubbleSort(int[] arr) { //TODO 冒泡排序 for(int i=ar ...
- Elasticsearch 结构化搜索、keyword、Term查询
前言 Elasticsearch 中的结构化搜索,即面向数值.日期.时间.布尔等类型数据的搜索,这些数据类型格式精确,通常使用基于词项的term精确匹配或者prefix前缀匹配.本文还将新版本的&qu ...