-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathDesign Movie Rental System.cpp
109 lines (76 loc) · 2.94 KB
/
Design Movie Rental System.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Runtime: 1596 ms (Top 70.59%) | Memory: 410.3 MB (Top 60.51%)
int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();
class MovieRentingSystem {
public:
#define pppi pair<int, pair <int, int>>
set < pppi > unrented,rented;
map < pair <int, int>, int > mp;
// facilitates search by storing the prices and shops a particular movie is associated with
unordered_map <int, set < pair <int, int> > > movies;
MovieRentingSystem(int n, vector<vector<int>>& entries) {
for(int i = 0; i < entries.size(); i++)
{
int shop = entries[i][0];
int movie = entries[i][1];
int price = entries[i][2];
// initially all movies are unrented
unrented.insert({price, {shop, movie}});
// store the price for each pair of {shop,movie}. remember this pair is unique as given in the problem statement
mp[{shop,movie}] = price;
// store all the movies with their price and shop
movies[movie].insert({price,shop});
}
}
vector<int> search(int movie) {
// return a list of 5 cheapest shops that have the given movie
vector <int> ans;
for(auto x : movies[movie])
{
if((int)ans.size()==5)
break;
ans.push_back(x.second);
}
return ans;
}
void rent(int shop, int movie) {
// remove the movie from the given shop
int price = mp[{shop,movie}];
// add this movie in rented
rented.insert({price, {shop, movie}});
// remove this movie from unrented
unrented.erase({price, {shop, movie}});
// also, erase this shop from the movies, since this movie will be no longer present in the given shop
movies[movie].erase(movies[movie].find({price,shop}));
}
void drop(int shop, int movie) {
// return or add
int price = mp[{shop,movie}];
// add in unrented as movie is back in the shop
unrented.insert({price, {shop, movie}});
movies[movie].insert({price,shop});
// remove from rented
rented.erase({price, {shop, movie}});
}
vector<vector<int>> report() {
// return the 5 cheapest rented movies as {shop, movie}
// here comes the use of rented data structure, since we can quickly select 5 cheapest movies that are rented
vector<vector<int>> ans;
for(auto x : rented)
{
if(ans.size() == 5)
break;
int shop = x.second.first;
int movie = x.second.second;
ans.push_back({shop,movie});
}
return ans;
}
};
/**
* Your MovieRentingSystem object will be instantiated and called as such:
* MovieRentingSystem* obj = new MovieRentingSystem(n, entries);
* vector<int> param_1 = obj->search(movie);
* obj->rent(shop,movie);
* obj->drop(shop,movie);
* vector<vector<int>> param_4 = obj->report();
*/