Skip to content

Commit 045ff75

Browse files
committed
Runtime: 1596 ms (Top 70.59%) | Memory: 410.3 MB (Top 60.51%)
1 parent d9734a0 commit 045ff75

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,98 @@
1+
// Runtime: 1596 ms (Top 70.59%) | Memory: 410.3 MB (Top 60.51%)
2+
13
int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();
24

35
class MovieRentingSystem {
46
public:
5-
6-
#define pppi pair<int, pair <int, int>>
7-
7+
8+
#define pppi pair<int, pair <int, int>>
9+
810
set < pppi > unrented,rented;
9-
11+
1012
map < pair <int, int>, int > mp;
11-
13+
1214
// facilitates search by storing the prices and shops a particular movie is associated with
1315
unordered_map <int, set < pair <int, int> > > movies;
14-
16+
1517
MovieRentingSystem(int n, vector<vector<int>>& entries) {
16-
18+
1719
for(int i = 0; i < entries.size(); i++)
1820
{
1921
int shop = entries[i][0];
2022
int movie = entries[i][1];
2123
int price = entries[i][2];
22-
24+
2325
// initially all movies are unrented
2426
unrented.insert({price, {shop, movie}});
25-
27+
2628
// store the price for each pair of {shop,movie}. remember this pair is unique as given in the problem statement
2729
mp[{shop,movie}] = price;
28-
30+
2931
// store all the movies with their price and shop
3032
movies[movie].insert({price,shop});
3133
}
3234
}
33-
35+
3436
vector<int> search(int movie) {
35-
37+
3638
// return a list of 5 cheapest shops that have the given movie
37-
39+
3840
vector <int> ans;
39-
41+
4042
for(auto x : movies[movie])
4143
{
4244
if((int)ans.size()==5)
4345
break;
44-
46+
4547
ans.push_back(x.second);
4648
}
47-
49+
4850
return ans;
4951
}
50-
52+
5153
void rent(int shop, int movie) {
5254
// remove the movie from the given shop
53-
55+
5456
int price = mp[{shop,movie}];
55-
57+
5658
// add this movie in rented
5759
rented.insert({price, {shop, movie}});
58-
60+
5961
// remove this movie from unrented
60-
unrented.erase({price, {shop, movie}});
61-
62+
unrented.erase({price, {shop, movie}});
63+
6264
// also, erase this shop from the movies, since this movie will be no longer present in the given shop
6365
movies[movie].erase(movies[movie].find({price,shop}));
6466
}
65-
67+
6668
void drop(int shop, int movie) {
6769
// return or add
68-
70+
6971
int price = mp[{shop,movie}];
7072

7173
// add in unrented as movie is back in the shop
7274
unrented.insert({price, {shop, movie}});
73-
75+
7476
movies[movie].insert({price,shop});
75-
77+
7678
// remove from rented
77-
rented.erase({price, {shop, movie}});
79+
rented.erase({price, {shop, movie}});
7880
}
79-
81+
8082
vector<vector<int>> report() {
8183
// return the 5 cheapest rented movies as {shop, movie}
82-
83-
// here comes the use of rented data structure, since we can quickly select 5 cheapest movies that are rented
84+
85+
// here comes the use of rented data structure, since we can quickly select 5 cheapest movies that are rented
8486
vector<vector<int>> ans;
85-
87+
8688
for(auto x : rented)
8789
{
8890
if(ans.size() == 5)
8991
break;
90-
91-
int shop = x.second.first;
92+
93+
int shop = x.second.first;
9294
int movie = x.second.second;
93-
95+
9496
ans.push_back({shop,movie});
9597
}
9698
return ans;
@@ -104,4 +106,4 @@ class MovieRentingSystem {
104106
* obj->rent(shop,movie);
105107
* obj->drop(shop,movie);
106108
* vector<vector<int>> param_4 = obj->report();
107-
*/
109+
*/

0 commit comments

Comments
 (0)