|
| 1 | +// Time: ctor: O(nlogn) |
| 2 | +// search: O(logn) |
| 3 | +// rent: O(logn) |
| 4 | +// drop: O(logn) |
| 5 | +// report: O(logn) |
| 6 | +// Space: O(n) |
| 7 | + |
| 8 | +class MovieRentingSystem { |
| 9 | +public: |
| 10 | + MovieRentingSystem(int n, vector<vector<int>>& entries) { |
| 11 | + for (const auto& e : entries) { |
| 12 | + movie_to_ordered_price_shop_[e[1]].emplace(e[2], e[0]); |
| 13 | + shop_movie_to_price_[e[0]][e[1]] = e[2]; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + vector<int> search(int movie) { |
| 18 | + vector<int> result; |
| 19 | + for (const auto& [_, s] : movie_to_ordered_price_shop_[movie]) { |
| 20 | + result.emplace_back(s); |
| 21 | + if (size(result) == 5) { |
| 22 | + break; |
| 23 | + } |
| 24 | + } |
| 25 | + return result; |
| 26 | + } |
| 27 | + |
| 28 | + void rent(int shop, int movie) { |
| 29 | + const auto price = shop_movie_to_price_[shop][movie]; |
| 30 | + movie_to_ordered_price_shop_[movie].erase(pair(price, shop)); |
| 31 | + rented_ordered_price_shop_movie_.emplace(price, shop, movie); |
| 32 | + } |
| 33 | + |
| 34 | + void drop(int shop, int movie) { |
| 35 | + const auto price = shop_movie_to_price_[shop][movie]; |
| 36 | + movie_to_ordered_price_shop_[movie].emplace(price, shop); |
| 37 | + rented_ordered_price_shop_movie_.erase({price, shop, movie}); |
| 38 | + } |
| 39 | + |
| 40 | + vector<vector<int>> report() { |
| 41 | + vector<vector<int>> result; |
| 42 | + for (const auto& [_, s, m] : rented_ordered_price_shop_movie_) { |
| 43 | + result.push_back({s, m}); |
| 44 | + if (size(result) == 5) { |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + return result; |
| 49 | + } |
| 50 | + |
| 51 | +private: |
| 52 | + unordered_map<int, set<pair<int, int>>> movie_to_ordered_price_shop_; |
| 53 | + unordered_map<int, unordered_map<int, int>> shop_movie_to_price_; |
| 54 | + set<tuple<int, int, int>> rented_ordered_price_shop_movie_; |
| 55 | +}; |
0 commit comments