Skip to content

Commit e33ee10

Browse files
add 939
1 parent 05ced80 commit e33ee10

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,5 @@ LeetCode
199199
|0936|[Stamping The Sequence](https://leetcode.com/problems/stamping-the-sequence/) | c | [c++](./src/0936-Stamping-The-Sequence/0936.cpp) |[python](./src/0936-Stamping-The-Sequence/0936.py)|||Hard|
200200
|0937|[Reorder Log Files](https://leetcode.com/problems/reorder-log-files/) | c | [c++](./src/0937-Reorder-Log-Files/0937.cpp) |[python](./src/0937-Reorder-Log-Files/0937.py)|||Easy|
201201
|0938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | c | [c++](./src/0938-Range-Sum-of-BST/0938.cpp) |[python](./src/0938-Range-Sum-of-BST/0938.py)|||Medium|
202-
|0939|[Minimum Area Rectangle](https://leetcode.com/problems/minimum-area-rectangle/) | c | [c++](./src/0939-Minimum-Area-Rectangle/0939.cpp) |[python](./src/0939-Minimum-Area-Rectangle/0939.py)|||Medium|
202+
|0939|[Minimum Area Rectangle](https://leetcode.com/problems/minimum-area-rectangle/) | c | [c++](./src/0939-Minimum-Area-Rectangle/0939.cpp) |[python](./src/0939-Minimum-Area-Rectangle/0939.py)|||Medium|
203+
|0940|[Distinct Subsequences II](https://leetcode.com/problems/distinct-subsequences-ii/) | c | [c++](./src/0940-Distinct-Subsequences-II/0940.cpp) |[python](./src/0940-Distinct-Subsequences-II/0940.py)|||Hard|
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <unordered_map>
4+
#include <set>
5+
#include <algorithm>
6+
using namespace std;
7+
8+
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
9+
class Solution
10+
{
11+
public:
12+
int minAreaRect(vector<vector<int>>& points)
13+
{
14+
int result = INT_MAX;
15+
unordered_map<int, set<int>> columns;
16+
for (auto point : points) columns[point[0]].insert(point[1]);
17+
for (auto x1 = columns.begin(); x1 != columns.end(); ++x1)
18+
{
19+
if (x1->second.size() < 2) continue;
20+
for (auto x2 = next(x1); x2 != columns.end(); ++x2)
21+
{
22+
if (x2->second.size() < 2) continue;
23+
vector<int> y;
24+
set_intersection(begin(x1->second), end(x1->second),
25+
begin(x2->second), end(x2->second), inserter(y, y.begin()));
26+
for (unsigned int i = 1; i < y.size(); ++i)
27+
result = min(result, abs(x2->first - x1->first) * (y[i] - y[i - 1]));
28+
}
29+
}
30+
return result == INT_MAX ? 0 : result;
31+
}
32+
};
33+
34+
35+
int main()
36+
{
37+
vector<vector<int>> points = {{1,1},{1,3},{3,1},{3,3},{4,1},{4,3}};
38+
cout << Solution().minAreaRect(points);
39+
return 0;
40+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def minAreaRect(self, points):
4+
"""
5+
:type points: List[List[int]]
6+
:rtype: int
7+
"""
8+
n = len(points)
9+
nx = len(set(x for x, y in points))
10+
ny = len(set(y for x, y in points))
11+
if nx == n or ny == n:
12+
return 0
13+
columns = defaultdict(list)
14+
for x, y in points:
15+
columns[x].append(y)
16+
17+
seen, result = {}, float('inf')
18+
19+
for x2 in sorted(columns):
20+
column = columns[x2]
21+
column.sort()
22+
for j, y2 in enumerate(column):
23+
for i in range(j):
24+
y1 = column[i]
25+
if (y1, y2) in seen:
26+
result = min(result, (x2 - seen[y1,y2]) * (y2 - y1))
27+
seen[y1, y2] = x2
28+
return result if result != float('inf') else 0
29+
30+
if __name__ == "__main__":
31+
points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
32+
print(Solution().minAreaRect(points))

0 commit comments

Comments
 (0)