Design Movie Rental System - Problem
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. - 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 as a 2D list where each entry is
[shop, movie], sorted by price, then by shop, then by movie.
Input & Output
Example 1 — Basic Movie Rental Operations
$
Input:
n = 3, entries = [[0,1,5],[0,2,6],[0,3,7],[1,1,4],[1,2,7],[2,1,5]], operations = [["search",1],["rent",1,1],["search",2],["report"]]
›
Output:
[[1,0,2],[],[0],[1,1]]
💡 Note:
Search movie 1: shops [1,0,2] by price [4,5,5]. Rent from shop 1. Search movie 2: shop [0]. Report: rented movie from shop 1 is [1,1].
Example 2 — Multiple Rent and Drop Operations
$
Input:
n = 2, entries = [[0,1,1],[0,2,2],[1,1,3]], operations = [["search",1],["rent",0,1],["drop",0,1],["search",1]]
›
Output:
[[0,1],[],[],[0,1]]
💡 Note:
Initially movie 1 available at shops [0,1] with prices [1,3]. After rent and drop, same availability restored.
Example 3 — Report with Multiple Movies
$
Input:
n = 2, entries = [[0,1,2],[1,1,3],[0,2,1]], operations = [["rent",0,1],["rent",1,1],["rent",0,2],["report"]]
›
Output:
[],[],[],[[0,2],[0,1],[1,1]]
💡 Note:
After renting 3 movies, report shows them sorted by price: $1 movie 2 from shop 0, $2 movie 1 from shop 0, $3 movie 1 from shop 1.
Constraints
- 1 ≤ n ≤ 3 × 104
- 1 ≤ entries.length ≤ 105
- 0 ≤ shopi < n
- 1 ≤ moviei, pricei ≤ 104
- Each shop carries at most one copy of a movie
- 1 ≤ operations.length ≤ 105
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code