题目如下:

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.

解题思路:本题是【leetcode】939. Minimum Area Rectangle 的升级版,由于题目要求矩形不需要和X轴或者Y轴平行,因此我的解法就是简单粗暴。从points中任意取出四个点判断是否是矩形,这样的话时间复杂度是O(n^4),虽然points.length最大只有50,但还是华丽丽的超时。 那就优化吧,我们可以以边为单位,假设两条边要组成矩形平行的边,那么这两条边的长度一定是相同的,所以可以实现把任意两个点组成的边的长度都计算出来,接下来再从长度相同的两条边中判断是否组成矩形,时间复杂度就降为O(n^2)了,和【leetcode】4Sum 的思路非常相似。如果两条边的长度相同,怎么判断是否能组成矩形呢,那就用勾股定理了,取这两条边的任意一点,分别计算出与其他三点的距离,判断是否满足勾股定理。看起来似乎是这样,但是有如下这种情况,红框点到两个绿框点的距离与红框点到蓝框点的距离是满足勾股定理的,但实际上这四个点并没有组成矩形。所以找出了四个点后,不能只判断其中一个点与其他三个点的距离,至少应该取其中两个来做判断(感觉是这样,但我不确定,所以我取了三个点判断),最后求出最小的面积即可。

代码如下:

class Solution(object):
def minAreaFreeRect(self, points):
"""
:type points: List[List[int]]
:rtype: float
"""
def dis(p1, p2):
return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
dic = {}
res = float('inf')
for i in range(len(points)):
for j in range(i+1,len(points)):
d = dis(points[i], points[j])
if d not in dic:
dic[d] = [(i,j)]
else:
dic[d].append((i,j))
for pl in dic.itervalues():
for i in range(len(pl)):
for j in range(i + 1, len(pl)):
if len(set(pl[i]) & set(pl[j])) > 0:
continue
p1,p2,p3,p4 = pl[i][0],pl[i][1],pl[j][0],pl[j][1]
d1 = dis(points[p1], points[p2])
d2 = dis(points[p1], points[p3])
d3 = dis(points[p1], points[p4])
dl = sorted([d1, d2, d3])
if dl[0] + dl[1] != dl[2]:
continue d1 = dis(points[p2], points[p1])
d2 = dis(points[p2], points[p3])
d3 = dis(points[p2], points[p4])
dl = sorted([d1, d2, d3])
if dl[0] + dl[1] != dl[2]:
continue d1 = dis(points[p3], points[p1])
d2 = dis(points[p3], points[p2])
d3 = dis(points[p3], points[p4])
dl = sorted([d1, d2, d3])
if dl[0] + dl[1] != dl[2]:
continue res = min(res, dl[0] * dl[1]) if res == float('inf'):
res = 0
import math
return math.sqrt(res)

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

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

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

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

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

  3. 【leetcode】939. Minimum Area Rectangle

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

  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】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  7. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

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

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. Quartz -----定时任务框架

    一.什么是Quartz     由java开发用来执行定时任务,类似于java.util.Timer.   但是相较于Timer,quartz增加了很多功能:                  持久性 ...

  2. 转载 Tomcat集群配置学习篇-----分布式应用

    Tomcat集群配置学习篇-----分布式应用 现目前基于javaWeb开发的应用系统已经比比皆是,尤其是电子商务网站,要想网站发展壮大,那么必然就得能够承受住庞大的网站访问量:大家知道如果服务器访问 ...

  3. Linux文本处理三剑客之——grep

    一Linux文本处理三剑客之——grep Linux文本处理三剑客都支持正则表达式 grep :文本过滤( 模式:pattern) 工具,包括grep, egrep, fgrep (不支持正则表达式) ...

  4. linux IPC 消息队列(二)

    我在网上想找多进程之间的通信方式,发现有人写的消息队列很好,搬过来: common.h #ifndef __COMMON_H_ #define __COMMON_H_ #include <std ...

  5. 洛谷 P2441 角色属性树

    题目描述 绪萌同人社是一个有趣的组织,该组织结构是一个树形结构.有一个社长,直接下属一些副社长.每个副社长又直接下属一些部长……. 每个成员都有一个萌点的属性,萌点属性是由一些质数的萌元素乘积构成(例 ...

  6. 2019牛客第八场多校 D_Distance 三维BIT或定期重建套路

    目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)D_Distance) 题意: 在三维空间\((n\times m\times h\le 100000)\)内,有\(q(q\le 100 ...

  7. Java第四次作业,面向对象高级特性(继承和多态)

    Java第四次作业-面向对象高级特性(继承和多态) (一)学习总结 1.学习使用思维导图对Java面向对象编程的知识点(封装.继承和多态)进行总结. 2.阅读下面程序,分析是否能编译通过?如果不能,说 ...

  8. 什么是AngularJs?特点是什么?和JQuery什么区别和联系

    什么是AngularJs? AngularJs是js框架,集中操作数据,不关注Dom操作,适用于以数据操作为主的的SPA(单页应用). 它的特点 采用MVC模型 双向数据绑定 依赖注入 模块化 与jQ ...

  9. python作业/练习/实战:3、实现商品管理的一个程序

    作业要求 实现一个商品管理的一个程序,运行程序有三个选项,输入1添加商品:输入2删除商品:输入3 查看商品信息1.添加商品: 商品名称:xx 商品如果已经存在,提示商品已存在 商品价格:xx数量只能为 ...

  10. python:TypeError: main() takes 0 positional arguments but 1 was given

    TypeError: main() takes 0 positional arguments but 1 was given def main(self): 括号里加上self就好了