Skip to content

Commit 2c26e09

Browse files
authored
Create design-movie-rental-system.py
1 parent 179c419 commit 2c26e09

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Python/design-movie-rental-system.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Time: ctor: O(nlogn)
2+
# search: O(logn)
3+
# rent: O(logn)
4+
# drop: O(logn)
5+
# report: O(logn)
6+
# Space: O(n)
7+
8+
import collections
9+
from sortedcontainers import SortedList
10+
11+
12+
class MovieRentingSystem(object):
13+
14+
def __init__(self, n, entries):
15+
"""
16+
:type n: int
17+
:type entries: List[List[int]]
18+
"""
19+
self.__movie_to_ordered_price_shop = collections.defaultdict(SortedList)
20+
self.__shop_movie_to_price = {}
21+
self.__rented_ordered_price_shop_movie = SortedList()
22+
for s, m, p in entries:
23+
self.__movie_to_ordered_price_shop[m].add((p, s))
24+
self.__shop_movie_to_price[s, m] = p
25+
26+
def search(self, movie):
27+
"""
28+
:type movie: int
29+
:rtype: List[int]
30+
"""
31+
return [s for _, s in self.__movie_to_ordered_price_shop[movie][:5]]
32+
33+
def rent(self, shop, movie):
34+
"""
35+
:type shop: int
36+
:type movie: int
37+
:rtype: None
38+
"""
39+
price = self.__shop_movie_to_price[shop, movie]
40+
self.__movie_to_ordered_price_shop[movie].remove((price, shop))
41+
self.__rented_ordered_price_shop_movie.add((price, shop, movie))
42+
43+
def drop(self, shop, movie):
44+
"""
45+
:type shop: int
46+
:type movie: int
47+
:rtype: None
48+
"""
49+
price = self.__shop_movie_to_price[shop, movie]
50+
self.__movie_to_ordered_price_shop[movie].add((price, shop))
51+
self.__rented_ordered_price_shop_movie.remove((price, shop, movie))
52+
53+
def report(self):
54+
"""
55+
:rtype: List[List[int]]
56+
"""
57+
return [[s, m] for _, s, m in self.__rented_ordered_price_shop_movie[:5]]

0 commit comments

Comments
 (0)