Skip to content

Added Solution for 1912. Movie Rental System in python. #1805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update Solution.py
  • Loading branch information
yanglbme authored Oct 15, 2023
commit d1241abfeaff70a0cc28922eb95bdf96631b241e
55 changes: 32 additions & 23 deletions solution/1900-1999/1912.Design Movie Rental System/Solution.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
from sortedcontainers import SortedList


class MovieRentingSystem:
def __init__(self, n: int, entries: List[List[int]]):
self.unrented = collections.defaultdict(SortedList) # {movie: (price, shop)}
self.shopAndMovieToPrice = {} # {(shop, movie): price}
self.rented = SortedList() # (price, shop, movie)
for shop, movie, price in entries:
self.unrented[movie].add((price, shop))
self.shopAndMovieToPrice[(shop, movie)] = price

def search(self, movie: int) -> List[int]:
return [shop for _, shop in self.unrented[movie][:5]]

def rent(self, shop: int, movie: int) -> None:
price = self.shopAndMovieToPrice[(shop, movie)]
self.unrented[movie].remove((price, shop))
self.rented.add((price, shop, movie))

def drop(self, shop: int, movie: int) -> None:
price = self.shopAndMovieToPrice[(shop, movie)]
self.unrented[movie].add((price, shop))
self.rented.remove((price, shop, movie))

def report(self) -> List[List[int]]:
return [[shop, movie] for _, shop, movie in self.rented[:5]]
def __init__(self, n: int, entries: List[List[int]]):
self.unrented = collections.defaultdict(SortedList) # {movie: (price, shop)}
self.shopAndMovieToPrice = {} # {(shop, movie): price}
self.rented = SortedList() # (price, shop, movie)
for shop, movie, price in entries:
self.unrented[movie].add((price, shop))
self.shopAndMovieToPrice[(shop, movie)] = price

def search(self, movie: int) -> List[int]:
return [shop for _, shop in self.unrented[movie][:5]]

def rent(self, shop: int, movie: int) -> None:
price = self.shopAndMovieToPrice[(shop, movie)]
self.unrented[movie].remove((price, shop))
self.rented.add((price, shop, movie))

def drop(self, shop: int, movie: int) -> None:
price = self.shopAndMovieToPrice[(shop, movie)]
self.unrented[movie].add((price, shop))
self.rented.remove((price, shop, movie))

def report(self) -> List[List[int]]:
return [[shop, movie] for _, shop, movie in self.rented[:5]]


# Your MovieRentingSystem object will be instantiated and called as such:
# obj = MovieRentingSystem(n, entries)
# param_1 = obj.search(movie)
# obj.rent(shop,movie)
# obj.drop(shop,movie)
# param_4 = obj.report()