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 <= 500 <= points[i][0] <= 400000 <= points[i][1] <= 40000- All points are distinct.
- Answers within
10^-5of 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 ...
随机推荐
- .Net Core 3.1浏览器后端服务(四) 你眼中的依赖注入与我相同吗?
一.前言 DI-Dependency Injection 依赖注入 IoC-Inversion of Control 控制反转 近几年这依赖注入. 控制反转已成为软件开发中不可或缺的一部分,那么该怎么 ...
- fastjson 漏洞利用 命令执行
目录 1. 准备一个Payload 2. 服务器上启动 rmi 3. 向目标注入payload 参考 如果你已经用DNSLog之类的工具,探测到了某个url有fastjson问题,那么接着可以试试能不 ...
- Java开发不懂Docker,学尽Java也枉然,阿里P8架构师手把手带你玩转Docker实战
转: Java开发不懂Docker,学尽Java也枉然,阿里P8架构师手把手带你玩转Docker实战 Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一 ...
- [BJWC2018] Kakuro
一.题目 点此看题 二.解法 我一开始一直想不出来,直接刚这个题实在是太复杂了,因为一开始就是不合法的. 下次遇到复杂的题一定要想 调整法 ,我再不往这个方向想我吔屎 好了言归正传,我们先找一组可行的 ...
- Vue 插槽之插槽内容学习总结
插槽内容使用方法介绍 父组件中引用支持插槽内容的子组件,形如以下(假设子组件为NavigationLink.vue) <navigation-link url="/profile&qu ...
- P1725 琪露诺 题解(单调队列)
题目链接 琪露诺 解题思路 单调队列优化的\(dp\). 状态转移方程:\(f[i]=max{f[i-l],f[i-l+1],...,f[i-r-1],f[i-r]}+a[i]\) 考虑单调队列优化. ...
- Servlet原理解析&使用指南
Servlet(Server Applet)全称Java Servlet,是用Java编写的服务器端程序.广义上指任何实现了Server接口的类,主要功能在于交互式地浏览和生成数据,生成动态Web内容 ...
- 一种借助POI粗略的标注城市也许重要的区域的方法
第一部分 很久以前,我住在村子里,因为村子小,所以对村子的一草一木都很熟悉,在熟悉的环境里就很有安全感. 后来我到了大城市,却发现城市太大了,一辈子都熟悉不完. 这个城市的绝大部分地方我都没有去过,就 ...
- L3-021 神坛 (叉积排序+向量积求面积)
题目链接 https://pintia.cn/problem-sets/994805046380707840/problems/994805046577840128 题意:给定n个点求三角形最小面积: ...
- Caused by: java.lang.RuntimeException: JxBrowser license check failed: No valid license found
使用jxbrower报错,原因时证书检验失败, 解决方案: 1.首先创建证书,下面是我在IDEA maven项目中创建的位置,Java项目中在src/目录下创建, META-INF/teamdev.l ...



