Skip to content

Commit 18868a5

Browse files
authored
Merge pull request SjxSubham#69 from shafaq16/patch-4
1912. Design Movie Rental System.cpp
2 parents a60aada + 6d02477 commit 18868a5

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using int2=pair<int,int>; // (price, shop)
2+
using int3=tuple<int,int,int>; // (price, shop, movie)
3+
4+
class MovieRentingSystem {
5+
public:
6+
unordered_map<uint64_t,int> Shop; // (shop,movie)->price
7+
unordered_map<int,set<int2>> Movie; // movie->set(price,shop)
8+
set<int3> rented; // rented->set (price,shop,movie)
9+
10+
static inline uint64_t key(int shop,int movie) {
11+
return (uint64_t)shop<<32|movie;
12+
}
13+
14+
MovieRentingSystem(int n, vector<vector<int>>& entries) {
15+
for (auto &e : entries) {
16+
int shop=e[0], movie=e[1], price=e[2];
17+
Shop[key(shop,movie)]=price;
18+
Movie[movie].insert({price, shop});
19+
}
20+
}
21+
22+
vector<int> search(int movie) {
23+
vector<int> ans;
24+
auto& S=Movie[movie];
25+
int i=0;
26+
for (auto it=S.begin(); it!=S.end() && i<5; it++, i++) {
27+
ans.push_back(it->second); // shop
28+
}
29+
return ans;
30+
}
31+
32+
void rent(int shop,int movie) {
33+
int price=Shop[key(shop,movie)];
34+
Movie[movie].erase({price, shop});
35+
rented.insert({price, shop, movie});
36+
}
37+
38+
void drop(int shop,int movie) {
39+
int price=Shop[key(shop,movie)];
40+
Movie[movie].insert({price, shop});
41+
rented.erase({price, shop, movie});
42+
}
43+
44+
vector<vector<int>> report() {
45+
vector<vector<int>> ans;
46+
int i=0;
47+
for (auto it=rented.begin(); it!=rented.end() && i<5; it++, i++) {
48+
auto [price, shop, movie]=*it;
49+
ans.push_back({shop, movie});
50+
}
51+
return ans;
52+
}
53+
};

0 commit comments

Comments
 (0)