Skip to content

Commit 7323c1f

Browse files
authored
Create minimum-area-rectangle-ii.py
1 parent ee319fe commit 7323c1f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Python/minimum-area-rectangle-ii.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time: O(n^2 ~ n^3)
2+
# Space: O(n^2)
3+
4+
import collections
5+
import itertools
6+
7+
8+
class Solution(object):
9+
def minAreaFreeRect(self, points):
10+
"""
11+
:type points: List[List[int]]
12+
:rtype: float
13+
"""
14+
points.sort()
15+
points = [complex(*z) for z in points]
16+
lookup = collections.defaultdict(list)
17+
for P, Q in itertools.combinations(points, 2):
18+
lookup[P-Q].append((P+Q) / 2)
19+
20+
result = float("inf")
21+
for A, candidates in lookup.iteritems():
22+
for P, Q in itertools.combinations(candidates, 2):
23+
if A.real * (P-Q).real + A.imag * (P-Q).imag == 0.0:
24+
result = min(result, abs(A) * abs(P-Q))
25+
return result if result < float("inf") else 0.0

0 commit comments

Comments
 (0)