[LeetCode] Design Movie Rental System

1912. Design Movie Rental System

You have a movie renting company consisting of n shops. You want to implement a renting system that supports searching for, booking, and returning movies. The system should also support generating a report of the currently rented movies.

Each movie is given as a 2D integer array entries where entries[i] = [shopi, moviei, pricei] indicates that there is a copy of movie moviei at shop shopi with a rental price of pricei. Each shop carries at most one copy of a movie moviei.

The system should support the following functions:

  • Search: Finds the cheapest 5 shops that have an unrented copy of a given movie. The shops should be sorted by price in ascending order, and in case of a tie, the one with the smaller shopi should appear first. If there are less than 5 matching shops, then all of them should be returned. If no shop has an unrented copy, then an empty list should be returned.
  • Rent: Rents an unrented copy of a given movie from a given shop.
  • Drop: Drops off a previously rented copy of a given movie at a given shop.
  • Report: Returns the cheapest 5 rented movies (possibly of the same movie ID) as a 2D list res where res[j] = [shopj, moviej] describes that the jth cheapest rented movie moviej was rented from the shop shopj. The movies in res should be sorted by price in ascending order, and in case of a tie, the one with the smaller shopj should appear first, and if there is still tie, the one with the smaller moviej should appear first. If there are fewer than 5 rented movies, then all of them should be returned. If no movies are currently being rented, then an empty list should be returned.

Implement the MovieRentingSystem class:

  • MovieRentingSystem(int n, int[][] entries) Initializes the MovieRentingSystem object with n shops and the movies in entries.
  • List search(int movie) Returns a list of shops that have an unrented copy of the given movie as described above.
  • void rent(int shop, int movie) Rents the given movie from the given shop.
  • void drop(int shop, int movie) Drops off a previously rented movie at the given shop.
  • List> report() Returns a list of cheapest rented movies as described above.

Note: The test cases will be generated such that rent will only be called if the shop has an unrented copy of the movie, and drop will only be called if the shop had previously rented out the movie.

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
class MovieRentingSystem {
unordered_map<int,set<pair<int,int>>> mov;
vector<unordered_map<int,pair<int,int>>> shp;

priority_queue<array<int,3>,vector<array<int,3>>,greater<array<int,3>>> pq;

bool duplicated(array<int,3>& a, array<int,3> b) {
return a == b;
}
public:
MovieRentingSystem(int n, vector<vector<int>>& entries) {
shp = vector<unordered_map<int,pair<int,int>>>(n);
for(auto& e : entries) {
auto s = e[0], m = e[1], p = e[2];
mov[m].insert({p,s});
shp[s][m] = {p,1};
}
}

vector<int> search(int movie) {
vector<int> res;
for(auto& [price, shop] : mov[movie]) {
if(res.size() == 5) break;
res.push_back(shop);
}
return res;
}



void rent(int shop, int movie) {
if(shp[shop].count(movie)) {
auto [p, h] = shp[shop][movie];
if(!h) return;
mov[movie].erase({p,shop});
shp[shop][movie].second = 0;
pq.push({p,shop,movie});
}
}

void drop(int shop, int movie) {
if(shp[shop].count(movie)) {
auto [p, h] = shp[shop][movie];
if(h) return;
mov[movie].insert({p,shop});
shp[shop][movie].second = 1;
}
}

vector<vector<int>> report() {
vector<array<int,3>> cheap;
while(!pq.empty() and cheap.size() < 5) {
auto [p, s, m] = pq.top(); pq.pop();
if(shp[s][m].second) continue;
if(!cheap.empty() and duplicated(cheap.back(), {p,s,m})) continue;
cheap.push_back({p,s,m});
}
vector<vector<int>> res;
for(auto& [p, s, m] : cheap) {
pq.push({p,s,m});
res.push_back({s,m});
}
return res;
}
};

/**
* 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();
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/27/PS/LeetCode/design-movie-rental-system/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.