1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| from collections import defaultdict from sortedcontainers import SortedList
class MovieRentingSystem:
def __init__(self, n: int, entries: List[List[int]]): self._prices = dict() self._available = defaultdict(SortedList) self._rent = SortedList() for shop, movie, price in entries: self._available[movie].add((price, shop)) self._prices[(shop, movie)] = price
def search(self, movie: int) -> List[int]: a = self._available if movie not in a: return [] return [shop for (price, shop) in a[movie][:5]]
def rent(self, shop: int, movie: int) -> None: price = self._prices[(shop, movie)] self._available[movie].discard((price, shop)) self._rent.add((price, shop, movie))
def drop(self, shop: int, movie: int) -> None: price = self._prices[(shop, movie)] self._rent.discard((price, shop, movie)) self._available[movie].add((price, shop))
def report(self) -> List[List[int]]: return [(shop, movie) for price, shop, movie in self._rent[:5]]
|