Skip to content
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

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

Merged
merged 4 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 34 additions & 0 deletions solution/1900-1999/1912.Design Movie Rental System/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,41 @@ movieRentingSystem.search(2); // 返回 [0, 1] 。商店 0 和 1 有未借出
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
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]]


# 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()
```

### **Java**
Expand Down
34 changes: 34 additions & 0 deletions solution/1900-1999/1912.Design Movie Rental System/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,41 @@ movieRentingSystem.search(2); // return [0, 1]. Movies of ID 2 are unrented at
### **Python3**

```python
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]]


# 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()
```

### **Java**
Expand Down
35 changes: 35 additions & 0 deletions solution/1900-1999/1912.Design Movie Rental System/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +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]]


# 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()