Design Movie Rental System Problem
Description
LeetCode Problem 1912.
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] = [shop_i, movie_i, price_i] indicates that there is a copy of movie movie_i at shop shop_i with a rental price of price_i. Each shop carries at most one copy of a movie movie_i.
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
shop_ishould 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
reswhereres[j] = [shop_j, movie_j]describes that thej^thcheapest rented moviemovie_jwas rented from the shopshop_j. The movies inresshould be sorted by price in ascending order, and in case of a tie, the one with the smallershop_jshould appear first, and if there is still tie, the one with the smallermovie_jshould 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 theMovieRentingSystemobject withnshops and the movies inentries.List<Integer> search(int movie)Returns a list of shops that have an unrented copy of the givenmovieas described above.void rent(int shop, int movie)Rents the givenmoviefrom the givenshop.void drop(int shop, int movie)Drops off a previously rentedmovieat the givenshop.List<List<Integer>> 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.
Example 1:
Input
["MovieRentingSystem", "search", "rent", "rent", "report", "drop", "search"]
[[3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]], [1], [0, 1], [1, 2], [], [1, 2], [2]]
Output
[null, [1, 0, 2], null, null, [[0, 1], [1, 2]], null, [0, 1]]
Explanation
MovieRentingSystem movieRentingSystem = new MovieRentingSystem(3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]);
movieRentingSystem.search(1); // return [1, 0, 2], Movies of ID 1 are unrented at shops 1, 0, and 2. Shop 1 is cheapest; shop 0 and 2 are the same price, so order by shop number.
movieRentingSystem.rent(0, 1); // Rent movie 1 from shop 0. Unrented movies at shop 0 are now [2,3].
movieRentingSystem.rent(1, 2); // Rent movie 2 from shop 1. Unrented movies at shop 1 are now [1].
movieRentingSystem.report(); // return [[0, 1], [1, 2]]. Movie 1 from shop 0 is cheapest, followed by movie 2 from shop 1.
movieRentingSystem.drop(1, 2); // Drop off movie 2 at shop 1. Unrented movies at shop 1 are now [1,2].
movieRentingSystem.search(2); // return [0, 1]. Movies of ID 2 are unrented at shops 0 and 1. Shop 0 is cheapest, followed by shop 1.
Constraints:
1 <= n <= 3 * 10^51 <= entries.length <= 10^50 <= shop_i < n1 <= movie_i, price_i <= 10^4- Each shop carries at most one copy of a movie
movie_i. - At most
10^5calls in total will be made tosearch,rent,dropandreport.
Solution
Approach: Ordered Sets
Every query asks for "the cheapest few, ties broken by id", so we keep the data sorted in the order the answers need.
available[movie]: an ordered set of(price, shop)for the shops that currently hold an unrented copy ofmovie.rented: one ordered set of(price, shop, movie)for every rented copy.priceMap: a hash map from(shop, movie)to its price, so thatrentanddropcan build the exact key to remove or insert.
The operations then become:
search(movie): read the first 5 entries ofavailable[movie].rent(shop, movie): move(price, shop)out ofavailable[movie]and(price, shop, movie)intorented.drop(shop, movie): do the reverse.report(): read the first 5 entries ofrented.
Languages without a built-in ordered set (Python, JavaScript, Go) use a sorted array with binary search; inserting or deleting in the middle costs a fast memory move, which is fine for 105 calls.
Complexity: with m entries, construction takes O(m log m), rent and drop take O(log m) (plus the array shift in the sorted-array versions), and search and report take O(1). Space is O(m).
from bisect import bisect_left, insort
from collections import defaultdict
class MovieRentingSystem:
def __init__(self, n: int, entries: List[List[int]]):
self.available = defaultdict(list) # movie -> sorted [(price, shop)]
self.price_map = {} # (shop, movie) -> price
for shop, movie, price in entries:
insort(self.available[movie], (price, shop))
self.price_map[(shop, movie)] = price
self.rented = [] # sorted [(price, shop, movie)]
def search(self, movie: int) -> List[int]:
return [shop for _, shop in self.available[movie][:5]]
def rent(self, shop: int, movie: int) -> None:
price = self.price_map[(shop, movie)]
lst = self.available[movie]
del lst[bisect_left(lst, (price, shop))]
insort(self.rented, (price, shop, movie))
def drop(self, shop: int, movie: int) -> None:
price = self.price_map[(shop, movie)]
del self.rented[bisect_left(self.rented, (price, shop, movie))]
insort(self.available[movie], (price, shop))
def report(self) -> List[List[int]]:
return [[shop, movie] for _, shop, movie in self.rented[:5]]
# Your MovieRentingSystem object will be instantiated and called as such:
# obj = MovieRentingSystem(n, entries)
# param_1 = obj.search(movie)
# obj.rent(shop,movie)
# obj.drop(shop,movie)
# param_4 = obj.report()