2023. Design Movie Rental System¶
Difficulty: Hard
LeetCode Problem View on GitHub
2023. Design Movie Rental System
Hard
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
shopishould 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] = [shopj, moviej]describes that thejthcheapest rented moviemoviejwas rented from the shopshopj. The movies inresshould be sorted by price in ascending order, and in case of a tie, the one with the smallershopjshould appear first, and if there is still tie, the one with the smallermoviejshould 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 * 1051 <= entries.length <= 1050 <= shopi < n1 <= moviei, pricei <= 104- Each shop carries at most one copy of a movie
moviei. - At most
105calls in total will be made tosearch,rent,dropandreport.
Solution¶
class MovieRentingSystem {
// Custom Pair class for shop-price mapping
static class Pair {
int movie, price; // 'movie' is overloaded to store shopId
public Pair(int movie, int price) {
this.movie = movie;
this.price = price;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Pair current = (Pair) obj;
return current.movie == movie && current.price == price;
}
@Override
public int hashCode() {
return Objects.hash(movie, price);
}
}
// Tuple class for complete movie information
static class Tuple {
int shop, movie, price;
public Tuple(int shop, int movie, int price) {
this.shop = shop;
this.movie = movie;
this.price = price;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Tuple current = (Tuple) obj;
return current.shop == shop &&
current.movie == movie &&
current.price == price;
}
@Override
public int hashCode() {
return Objects.hash(shop, movie, price);
}
}
// Comparator for sorting Tuples
static class customSortTuple implements Comparator<Tuple> {
@Override
public int compare(Tuple first, Tuple second) {
int priceCmp = Integer.compare(first.price, second.price);
if (priceCmp != 0) return priceCmp;
int shopCmp = Integer.compare(first.shop, second.shop);
if (shopCmp != 0) return shopCmp;
return Integer.compare(first.movie, second.movie);
}
}
// Comparator for sorting shops by price
static class customSortShop implements Comparator<Pair> {
@Override
public int compare(Pair first, Pair second) {
int priceCmp = Integer.compare(first.price, second.price);
if (priceCmp != 0) return priceCmp;
return Integer.compare(first.movie, second.movie); // movie stores shopId
}
}
private HashMap<Pair, Integer> shopMoviePrice;
private HashMap<Integer, TreeSet<Pair>> unRentedMap;
private TreeSet<Tuple> rentedMovies;
public MovieRentingSystem(int n, int[][] entries) {
shopMoviePrice = new HashMap<>();
rentedMovies = new TreeSet<>(new customSortTuple());
unRentedMap = new HashMap<>();
for (int[] entry : entries) {
int shop = entry[0], movie = entry[1], price = entry[2];
shopMoviePrice.put(new Pair(shop, movie), price);
if (!unRentedMap.containsKey(movie)) {
unRentedMap.put(movie, new TreeSet<>(new customSortShop()));
}
unRentedMap.get(movie).add(new Pair(shop, price));
}
}
public List<Integer> search(int movie) {
TreeSet<Pair> current = unRentedMap.get(movie);
List<Integer> res = new ArrayList<>();
if (current == null) return res;
Iterator<Pair> it = current.iterator();
while (res.size() < 5 && it.hasNext()) {
res.add(it.next().movie); // movie field holds shopId
}
return res;
}
public void rent(int shop, int movie) {
int price = shopMoviePrice.get(new Pair(shop, movie));
Tuple t = new Tuple(shop, movie, price);
rentedMovies.add(t);
TreeSet<Pair> set = unRentedMap.get(movie);
if (set != null) {
set.remove(new Pair(shop, price));
}
}
public void drop(int shop, int movie) {
int price = shopMoviePrice.get(new Pair(shop, movie));
Tuple t = new Tuple(shop, movie, price);
rentedMovies.remove(t);
if (!unRentedMap.containsKey(movie)) {
unRentedMap.put(movie, new TreeSet<>(new customSortShop()));
}
unRentedMap.get(movie).add(new Pair(shop, price));
}
public List<List<Integer>> report() {
List<List<Integer>> res = new ArrayList<>();
Iterator<Tuple> it = rentedMovies.iterator();
while (res.size() < 5 && it.hasNext()) {
Tuple current = it.next();
res.add(Arrays.asList(current.shop, current.movie));
}
return res;
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here