forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-movie-rental-system.py
57 lines (49 loc) · 1.65 KB
/
design-movie-rental-system.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Time: ctor: O(nlogn)
# search: O(logn)
# rent: O(logn)
# drop: O(logn)
# report: O(logn)
# Space: O(n)
import collections
from sortedcontainers import SortedList
class MovieRentingSystem(object):
def __init__(self, n, entries):
"""
:type n: int
:type entries: List[List[int]]
"""
self.__movie_to_ordered_price_shop = collections.defaultdict(SortedList)
self.__shop_movie_to_price = {}
self.__rented_ordered_price_shop_movie = SortedList()
for s, m, p in entries:
self.__movie_to_ordered_price_shop[m].add((p, s))
self.__shop_movie_to_price[s, m] = p
def search(self, movie):
"""
:type movie: int
:rtype: List[int]
"""
return [s for _, s in self.__movie_to_ordered_price_shop[movie][:5]]
def rent(self, shop, movie):
"""
:type shop: int
:type movie: int
:rtype: None
"""
price = self.__shop_movie_to_price[shop, movie]
self.__movie_to_ordered_price_shop[movie].remove((price, shop))
self.__rented_ordered_price_shop_movie.add((price, shop, movie))
def drop(self, shop, movie):
"""
:type shop: int
:type movie: int
:rtype: None
"""
price = self.__shop_movie_to_price[shop, movie]
self.__movie_to_ordered_price_shop[movie].add((price, shop))
self.__rented_ordered_price_shop_movie.remove((price, shop, movie))
def report(self):
"""
:rtype: List[List[int]]
"""
return [[s, m] for _, s, m in self.__rented_ordered_price_shop_movie[:5]]