- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 29 lines of Python from the credited upstream file 1912.py.
- The implementation visibly relies on sequence storage, hash lookup.
- No explicit loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1from sortedcontainers import SortedList2 3 4class MovieRentingSystem:5 def __init__(self, n: int, entries: list[list[int]]):6 self.unrented = collections.defaultdict(7 SortedList) 8 self.shopAndMovieToPrice = {} 9 self.rented = SortedList() 10 for shop, movie, price in entries:11 self.unrented[movie].add((price, shop))12 self.shopAndMovieToPrice[(shop, movie)] = price13 14 def search(self, movie: int) -> list[int]:15 return [shop for _, shop in self.unrented[movie][:5]]16 17 def rent(self, shop: int, movie: int) -> None:18 price = self.shopAndMovieToPrice[(shop, movie)]19 self.unrented[movie].remove((price, shop))20 self.rented.add((price, shop, movie))21 22 def drop(self, shop: int, movie: int) -> None:23 price = self.shopAndMovieToPrice[(shop, movie)]24 self.unrented[movie].add((price, shop))25 self.rented.remove((price, shop, movie))26 27 def report(self) -> list[list[int]]:28 return [[shop, movie] for _, shop, movie in self.rented[:5]]29