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. Java常用类:Arrays类

    一.简介 全类名:java.util.Arrays 描述: 此类包含用来操作数组(比如排序和搜索)的各种方法. 此类还包含一个允许将数组作为列表来查看的静态工厂. 注意: 除非特别注明,否则如果指定数 ...

  2. 剑指 Offer 26. 树的子结构

    剑指 Offer 26. 树的子结构 Offer 26 题目详情: 题解分析 解法一: 第一种比较容易想到的解法就是查看这两棵树的前序遍历和中序遍历序列是否都匹配. 因为前序遍历和中序遍历可以唯一确定 ...

  3. HDOJ-1754(线段树+单点更新)

    I Hate It HDOJ-1754 这道题是线段树简单的入门题,只是简单考察了线段树的基本使用,建树等操作. 这里需要注意的是输入要不使用scanf要不使用快速输入. 这里的maxs数组需要开大一 ...

  4. Hadoop的常用命令

    注:hadoop的使用命令包含 hadoop fs 开头 or hdfs dfs开头 等多种形式来操作. 这里以hadoo fs的形式来介绍在这些命令 1.列出根目录下所有的目录或文件 hadoop ...

  5. 2020年12月-第02阶段-前端基础-CSS Day04

    1. 浮动(float) 记忆 能够说出 CSS 的布局的三种机制 理解 能够说出普通流在布局中的特点 能够说出我们为什么用浮动 能够说出我们为什么要清除浮动 应用 能够利用浮动完成导航栏案例 能够清 ...

  6. mysql基本指令2

    pymysql:  - 连接.关闭(游标)  - execute()   -- SQL注入    sss' or 1=1 --   - 增删改: conn.commit()  - fetchone f ...

  7. mysql内一些可以延时注入的查询语句

        一.sleep() 配合其他函数进行使用将十分方便,如下所示: 拆分讲解: select substr(database(),1,1) ; 截取当前数据库的第一位,转换为ascii码值: se ...

  8. 趣谈 DHCP 协议,有点意思。

    计算机网络我也连载了很多篇了,大家可以在我的公众号「程序员cxuan」 或者我的 github 系统学习. 计算机网络第一篇,聊一聊网络基础 :计算机网络基础知识总结 计算机网络第二篇,聊一聊 TCP ...

  9. Memory Networks02 记忆网络经典论文

    目录 1 Recurrent Entity Network Introduction 模型构建 Input Encoder Dynamic Memory Output Model 总结 2 hiera ...

  10. malloc和free解析

    malloc和free都是库函数,调用系统函数sbrk()来分配内存.除了分配可使用的内存以外,还分配了"控制"信息,这有点像内存池常用的手段.并且,分配的内存是连续的. 1. m ...