Skip to content

Commit d72d9e4

Browse files
nrhitikyanglbme
andauthored
feat: add python solution to lc problem: No.1912 (#1805)
No.1912.Design Movie Rental System --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 7ea8094 commit d72d9e4

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

solution/1900-1999/1912.Design Movie Rental System/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,41 @@ movieRentingSystem.search(2); // 返回 [0, 1] 。商店 0 和 1 有未借出
7676
<!-- 这里可写当前语言的特殊实现逻辑 -->
7777

7878
```python
79+
from sortedcontainers import SortedList
7980

81+
82+
class MovieRentingSystem:
83+
def __init__(self, n: int, entries: List[List[int]]):
84+
self.unrented = collections.defaultdict(SortedList) # {movie: (price, shop)}
85+
self.shopAndMovieToPrice = {} # {(shop, movie): price}
86+
self.rented = SortedList() # (price, shop, movie)
87+
for shop, movie, price in entries:
88+
self.unrented[movie].add((price, shop))
89+
self.shopAndMovieToPrice[(shop, movie)] = price
90+
91+
def search(self, movie: int) -> List[int]:
92+
return [shop for _, shop in self.unrented[movie][:5]]
93+
94+
def rent(self, shop: int, movie: int) -> None:
95+
price = self.shopAndMovieToPrice[(shop, movie)]
96+
self.unrented[movie].remove((price, shop))
97+
self.rented.add((price, shop, movie))
98+
99+
def drop(self, shop: int, movie: int) -> None:
100+
price = self.shopAndMovieToPrice[(shop, movie)]
101+
self.unrented[movie].add((price, shop))
102+
self.rented.remove((price, shop, movie))
103+
104+
def report(self) -> List[List[int]]:
105+
return [[shop, movie] for _, shop, movie in self.rented[:5]]
106+
107+
108+
# Your MovieRentingSystem object will be instantiated and called as such:
109+
# obj = MovieRentingSystem(n, entries)
110+
# param_1 = obj.search(movie)
111+
# obj.rent(shop,movie)
112+
# obj.drop(shop,movie)
113+
# param_4 = obj.report()
80114
```
81115

82116
### **Java**

solution/1900-1999/1912.Design Movie Rental System/README_EN.md

+34
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,41 @@ movieRentingSystem.search(2); // return [0, 1]. Movies of ID 2 are unrented at
6868
### **Python3**
6969

7070
```python
71+
from sortedcontainers import SortedList
7172

73+
74+
class MovieRentingSystem:
75+
def __init__(self, n: int, entries: List[List[int]]):
76+
self.unrented = collections.defaultdict(SortedList) # {movie: (price, shop)}
77+
self.shopAndMovieToPrice = {} # {(shop, movie): price}
78+
self.rented = SortedList() # (price, shop, movie)
79+
for shop, movie, price in entries:
80+
self.unrented[movie].add((price, shop))
81+
self.shopAndMovieToPrice[(shop, movie)] = price
82+
83+
def search(self, movie: int) -> List[int]:
84+
return [shop for _, shop in self.unrented[movie][:5]]
85+
86+
def rent(self, shop: int, movie: int) -> None:
87+
price = self.shopAndMovieToPrice[(shop, movie)]
88+
self.unrented[movie].remove((price, shop))
89+
self.rented.add((price, shop, movie))
90+
91+
def drop(self, shop: int, movie: int) -> None:
92+
price = self.shopAndMovieToPrice[(shop, movie)]
93+
self.unrented[movie].add((price, shop))
94+
self.rented.remove((price, shop, movie))
95+
96+
def report(self) -> List[List[int]]:
97+
return [[shop, movie] for _, shop, movie in self.rented[:5]]
98+
99+
100+
# Your MovieRentingSystem object will be instantiated and called as such:
101+
# obj = MovieRentingSystem(n, entries)
102+
# param_1 = obj.search(movie)
103+
# obj.rent(shop,movie)
104+
# obj.drop(shop,movie)
105+
# param_4 = obj.report()
72106
```
73107

74108
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class MovieRentingSystem:
5+
def __init__(self, n: int, entries: List[List[int]]):
6+
self.unrented = collections.defaultdict(SortedList) # {movie: (price, shop)}
7+
self.shopAndMovieToPrice = {} # {(shop, movie): price}
8+
self.rented = SortedList() # (price, shop, movie)
9+
for shop, movie, price in entries:
10+
self.unrented[movie].add((price, shop))
11+
self.shopAndMovieToPrice[(shop, movie)] = price
12+
13+
def search(self, movie: int) -> List[int]:
14+
return [shop for _, shop in self.unrented[movie][:5]]
15+
16+
def rent(self, shop: int, movie: int) -> None:
17+
price = self.shopAndMovieToPrice[(shop, movie)]
18+
self.unrented[movie].remove((price, shop))
19+
self.rented.add((price, shop, movie))
20+
21+
def drop(self, shop: int, movie: int) -> None:
22+
price = self.shopAndMovieToPrice[(shop, movie)]
23+
self.unrented[movie].add((price, shop))
24+
self.rented.remove((price, shop, movie))
25+
26+
def report(self) -> List[List[int]]:
27+
return [[shop, movie] for _, shop, movie in self.rented[:5]]
28+
29+
30+
# Your MovieRentingSystem object will be instantiated and called as such:
31+
# obj = MovieRentingSystem(n, entries)
32+
# param_1 = obj.search(movie)
33+
# obj.rent(shop,movie)
34+
# obj.drop(shop,movie)
35+
# param_4 = obj.report()

0 commit comments

Comments
 (0)