Skip to content

Commit f055be9

Browse files
committed
[LeetCode Sync] Runtime - 391 ms (89.79%), Memory - 16.8 MB (72.94%)
1 parent c7e237e commit f055be9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p>You are given an array of points in the <strong>X-Y</strong> plane <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
2+
3+
<p>Return <em>the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes</em>. If there is not any such rectangle, return <code>0</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/rec1.JPG" style="width: 500px; height: 447px;" />
8+
<pre>
9+
<strong>Input:</strong> points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
10+
<strong>Output:</strong> 4
11+
</pre>
12+
13+
<p><strong class="example">Example 2:</strong></p>
14+
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/03/rec2.JPG" style="width: 500px; height: 477px;" />
15+
<pre>
16+
<strong>Input:</strong> points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
17+
<strong>Output:</strong> 2
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>1 &lt;= points.length &lt;= 500</code></li>
25+
<li><code>points[i].length == 2</code></li>
26+
<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 4 * 10<sup>4</sup></code></li>
27+
<li>All the given points are <strong>unique</strong>.</li>
28+
</ul>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minAreaRect(self, points: List[List[int]]) -> int:
3+
hashmap = collections.defaultdict(set)
4+
for point in points:
5+
hashmap[point[0]].add(point[1])
6+
dict_x = {x: set_y for x, set_y in hashmap.items() if len(set_y) > 1}
7+
min_area = float('inf')
8+
for x1, x2 in combinations(dict_x.keys(), 2):
9+
for y1, y2 in combinations(dict_x[x1] & dict_x[x2], 2):
10+
min_area = min(min_area, abs(x1 - x2) * abs(y1 - y2))
11+
return min_area if min_area < float('inf') else 0

0 commit comments

Comments
 (0)