aboutsummaryrefslogtreecommitdiff
path: root/design_movie_rental.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-09-20 22:27:01 -0600
committerJack Sangdahl <[email protected]>2025-09-20 22:27:01 -0600
commit927ff9c0b810a6dbcf12f2389423e2cf0200c7c1 (patch)
tree65223f526f2293172060c6964a07a7ebcc9a1a46 /design_movie_rental.py
parentd5e6bc72a4a72b64bb237fe8a1c22cae73f0dcab (diff)
downloadleetcode-927ff9c0b810a6dbcf12f2389423e2cf0200c7c1.tar.gz
leetcode-927ff9c0b810a6dbcf12f2389423e2cf0200c7c1.zip
1912. design movie rental system
Diffstat (limited to 'design_movie_rental.py')
-rw-r--r--design_movie_rental.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/design_movie_rental.py b/design_movie_rental.py
new file mode 100644
index 0000000..384ed12
--- /dev/null
+++ b/design_movie_rental.py
@@ -0,0 +1,63 @@
+# 1912. Design Movie Rental System
+from collections import defaultdict
+
+from sortedcontainers import SortedList
+
+
+class MovieRentingSystem(object):
+ def __init__(self, n, entries):
+ """
+ :type n: int
+ :type entries: List[List[int]]
+ """
+ self.shops = defaultdict(SortedList)
+ self.shop_movie = {}
+ self.rented = SortedList()
+ for s, m, p in entries:
+ self.shops[m].add((p, s))
+ self.shop_movie[s, m] = p
+
+ def search(self, movie):
+ """
+ :type movie: int
+ :rtype: List[int]
+ """
+ return [i for _, i in self.shops[movie][:5]]
+
+ def rent(self, shop, movie):
+ """
+ :type shop: int
+ :type movie: int
+ :rtype: None
+ """
+ price = self.shop_movie[shop, movie]
+ self.shops[movie].remove((price, shop))
+ self.rented.add((price, shop, movie))
+
+ def drop(self, shop, movie):
+ """
+ :type shop: int
+ :type movie: int
+ :rtype: None
+ """
+ price = self.shop_movie[shop, movie]
+ self.shops[movie].add((price, shop))
+ self.rented.remove((price, shop, movie))
+
+ def report(self):
+ """
+ :rtype: List[List[int]]
+ """
+ return [[i, j] for _, i, j in self.rented[:5]]
+
+
+if __name__ == "__main__":
+ obj = MovieRentingSystem(
+ 3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]
+ )
+ print(obj.search(1))
+ obj.rent(0, 1)
+ obj.rent(1, 2)
+ print(obj.report())
+ obj.drop(1, 2)
+ print(obj.search(2))