|
| 1 | +class MovieRentingSystem(object): |
| 2 | + |
| 3 | + def __init__(self, n, entries): |
| 4 | + """ |
| 5 | + :type n: int |
| 6 | + :type entries: List[List[int]] |
| 7 | + """ |
| 8 | + self.avail = defaultdict(dict) |
| 9 | + self.prices = defaultdict(dict) |
| 10 | + self.rented = [] |
| 11 | + |
| 12 | + for s, m, p in entries: |
| 13 | + self.avail[m][s] = p |
| 14 | + self.prices[m][s] = p |
| 15 | + |
| 16 | + |
| 17 | + def search(self, movie): |
| 18 | + shop_inventory = self.avail[movie] |
| 19 | + if len(shop_inventory) == 0: |
| 20 | + return [] |
| 21 | + |
| 22 | + return [m[0] for m in sorted(shop_inventory.items(), key=lambda(k,v): (v,k))][:5] |
| 23 | + |
| 24 | + |
| 25 | + def rent(self, shop, movie): |
| 26 | + price = self.avail[movie][shop] |
| 27 | + self.avail[movie].pop(shop) |
| 28 | + |
| 29 | + self.rented.append([price, shop, movie]) |
| 30 | + |
| 31 | + |
| 32 | + def drop(self, shop, movie): |
| 33 | + price = self.prices[movie][shop] |
| 34 | + |
| 35 | + self.rented.remove([price, shop, movie]) |
| 36 | + self.avail[movie][shop] = price |
| 37 | + |
| 38 | + |
| 39 | + def report(self): |
| 40 | + sorted_output = sorted(self.rented)[:5] |
| 41 | + return map(lambda x: x[-2:], sorted_output) |
0 commit comments