From 958ffd3bd790b50f41f1350fcba8b24cb209ae77 Mon Sep 17 00:00:00 2001 From: chayan das Date: Mon, 22 Sep 2025 00:02:48 +0530 Subject: [PATCH] Create 1912. Design Movie Rental System --- 1912. Design Movie Rental System | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 1912. Design Movie Rental System diff --git a/1912. Design Movie Rental System b/1912. Design Movie Rental System new file mode 100644 index 0000000..64dbb4a --- /dev/null +++ b/1912. Design Movie Rental System @@ -0,0 +1,78 @@ +struct unrentedComparator { + bool operator()(const pair& p1,const pair& p2) const { + if(p1.second==p2.second){ + return p1.first& t1,const tuple& t2) const { + // tuple = {price, shop, movie} + if(get<0>(t1)==get<0>(t2)){ + if(get<1>(t1)==get<1>(t2)){ + return get<2>(t1)(t2); + } + return get<1>(t1)(t2); + } + return get<0>(t1)(t2); + } +}; + +class MovieRentingSystem { + // movie -> set of {shop, price}, sorted by (price, shop) + unordered_map,unrentedComparator>> unrentedMovie; + + // rented movies = set of {price, shop, movie} + set,rentedComparator> rentedMovie; + + unordered_map priceMap; // "movie_shop" -> price + + string getId(int movie,int shop){ + return to_string(movie)+"_"+to_string(shop); + } + +public: + MovieRentingSystem(int n, vector>& entries) { + for(auto &e: entries){ + int shop=e[0], movie=e[1], price=e[2]; + unrentedMovie[movie].insert({shop,price}); + string id=getId(movie,shop); + priceMap[id]=price; + } + } + + vector search(int movie) { + vector shops; + if(unrentedMovie.find(movie)==unrentedMovie.end()){ + return shops; + } + for(auto [shop,price]: unrentedMovie[movie]){ + shops.push_back(shop); + if(shops.size()==5) break; + } + return shops; + } + + void rent(int shop, int movie) { + int price=priceMap[getId(movie,shop)]; + unrentedMovie[movie].erase({shop,price}); + rentedMovie.insert({price,shop,movie}); + } + + void drop(int shop, int movie) { + int price=priceMap[getId(movie,shop)]; + rentedMovie.erase({price,shop,movie}); + unrentedMovie[movie].insert({shop,price}); + } + + vector> report() { + vector> reportResult; + for(auto &[price,shop,movie]: rentedMovie){ + reportResult.push_back({shop,movie}); + if(reportResult.size()==5) break; + } + return reportResult; + } +};