From ef6cff50ccf578af24d55565b631fd99956db3cb Mon Sep 17 00:00:00 2001 From: Rhitik Date: Sat, 14 Oct 2023 21:04:18 +0530 Subject: [PATCH 1/5] Added Solution for 1912. Movie Rental System in python. --- .../1912.Design Movie Rental System/README.md | 26 ++++++++++++++++++ .../README_EN.md | 27 ++++++++++++++++++- .../Solution.py | 26 ++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 solution/1900-1999/1912.Design Movie Rental System/Solution.py diff --git a/solution/1900-1999/1912.Design Movie Rental System/README.md b/solution/1900-1999/1912.Design Movie Rental System/README.md index 0a8c0c09ec8e8..6238f8e94c651 100644 --- a/solution/1900-1999/1912.Design Movie Rental System/README.md +++ b/solution/1900-1999/1912.Design Movie Rental System/README.md @@ -76,6 +76,32 @@ 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]] ``` diff --git a/solution/1900-1999/1912.Design Movie Rental System/README_EN.md b/solution/1900-1999/1912.Design Movie Rental System/README_EN.md index b5f20dc46734f..18a5f67c40a28 100644 --- a/solution/1900-1999/1912.Design Movie Rental System/README_EN.md +++ b/solution/1900-1999/1912.Design Movie Rental System/README_EN.md @@ -68,7 +68,32 @@ 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]] ``` ### **Java** diff --git a/solution/1900-1999/1912.Design Movie Rental System/Solution.py b/solution/1900-1999/1912.Design Movie Rental System/Solution.py new file mode 100644 index 0000000000000..3f0d1211a48b5 --- /dev/null +++ b/solution/1900-1999/1912.Design Movie Rental System/Solution.py @@ -0,0 +1,26 @@ +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]] From ca705c1a5219ad38c6314b32291499de6bd797be Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 15 Oct 2023 09:18:27 +0800 Subject: [PATCH 2/5] Update README.md --- .../1912.Design Movie Rental System/README.md | 56 +++++++++++-------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/solution/1900-1999/1912.Design Movie Rental System/README.md b/solution/1900-1999/1912.Design Movie Rental System/README.md index 6238f8e94c651..569ffe773e617 100644 --- a/solution/1900-1999/1912.Design Movie Rental System/README.md +++ b/solution/1900-1999/1912.Design Movie Rental System/README.md @@ -78,31 +78,39 @@ 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]] +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** From 727bd2c6d20bb8c8fd7916b0414064eba09aa21f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 15 Oct 2023 09:18:46 +0800 Subject: [PATCH 3/5] Update README_EN.md --- .../README_EN.md | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/solution/1900-1999/1912.Design Movie Rental System/README_EN.md b/solution/1900-1999/1912.Design Movie Rental System/README_EN.md index 18a5f67c40a28..8276a456cdb36 100644 --- a/solution/1900-1999/1912.Design Movie Rental System/README_EN.md +++ b/solution/1900-1999/1912.Design Movie Rental System/README_EN.md @@ -70,30 +70,39 @@ movieRentingSystem.search(2); // return [0, 1]. Movies of ID 2 are unrented at ```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]] + 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** From d1241abfeaff70a0cc28922eb95bdf96631b241e Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 15 Oct 2023 09:27:54 +0800 Subject: [PATCH 4/5] Update Solution.py --- .../Solution.py | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/solution/1900-1999/1912.Design Movie Rental System/Solution.py b/solution/1900-1999/1912.Design Movie Rental System/Solution.py index 3f0d1211a48b5..3f309835db942 100644 --- a/solution/1900-1999/1912.Design Movie Rental System/Solution.py +++ b/solution/1900-1999/1912.Design Movie Rental System/Solution.py @@ -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() From a02c4095bda9b8fd96b86b42777a6606dc80fd5c Mon Sep 17 00:00:00 2001 From: Rhitik Date: Mon, 16 Oct 2023 01:07:12 +0530 Subject: [PATCH 5/5] Solution for 1900. Earliest and Latest Rounds Where Players Compete. --- .../README.md | 28 +++++++++++++++++++ .../README_EN.md | 28 +++++++++++++++++++ .../Solution.py | 28 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/Solution.py diff --git a/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README.md b/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README.md index 254647517b86f..50143e1ed8128 100644 --- a/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README.md +++ b/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README.md @@ -74,6 +74,34 @@ ```python +class Solution: + def earliestAndLatest( + self, n: int, firstPlayer: int, secondPlayer: int + ) -> List[int]: + # dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from + # Front, secondPlayer is j-th player from end, and there're k people + @functools.lru_cache(None) + def dp(l: int, r: int, k: int) -> List[int]: + if l == r: + return [1, 1] + if l > r: + return dp(r, l, k) + + a = math.inf + b = -math.inf + + # Enumerate all possible positions + for i in range(1, l + 1): + for j in range(l - i + 1, r - i + 1): + if not l + r - k // 2 <= i + j <= (k + 1) // 2: + continue + x, y = dp(i, j, (k + 1) // 2) + a = min(a, x + 1) + b = max(b, y + 1) + + return [a, b] + + return dp(firstPlayer, n - secondPlayer + 1, n) ``` diff --git a/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README_EN.md b/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README_EN.md index 44caf76986813..35a38bf73fbfc 100644 --- a/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README_EN.md +++ b/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/README_EN.md @@ -68,6 +68,34 @@ There is no way to make them compete in any other round. ### **Python3** ```python +class Solution: + def earliestAndLatest( + self, n: int, firstPlayer: int, secondPlayer: int + ) -> List[int]: + # dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from + # Front, secondPlayer is j-th player from end, and there're k people + @functools.lru_cache(None) + def dp(l: int, r: int, k: int) -> List[int]: + if l == r: + return [1, 1] + if l > r: + return dp(r, l, k) + + a = math.inf + b = -math.inf + + # Enumerate all possible positions + for i in range(1, l + 1): + for j in range(l - i + 1, r - i + 1): + if not l + r - k // 2 <= i + j <= (k + 1) // 2: + continue + x, y = dp(i, j, (k + 1) // 2) + a = min(a, x + 1) + b = max(b, y + 1) + + return [a, b] + + return dp(firstPlayer, n - secondPlayer + 1, n) ``` diff --git a/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/Solution.py b/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/Solution.py new file mode 100644 index 0000000000000..f16f724b7a0ab --- /dev/null +++ b/solution/1900-1999/1900.The Earliest and Latest Rounds Where Players Compete/Solution.py @@ -0,0 +1,28 @@ +class Solution: + def earliestAndLatest( + self, n: int, firstPlayer: int, secondPlayer: int + ) -> List[int]: + # dp[i][j][k] := (earliest, latest) pair w/ firstPlayer is i-th player from + # Front, secondPlayer is j-th player from end, and there're k people + @functools.lru_cache(None) + def dp(l: int, r: int, k: int) -> List[int]: + if l == r: + return [1, 1] + if l > r: + return dp(r, l, k) + + a = math.inf + b = -math.inf + + # Enumerate all possible positions + for i in range(1, l + 1): + for j in range(l - i + 1, r - i + 1): + if not l + r - k // 2 <= i + j <= (k + 1) // 2: + continue + x, y = dp(i, j, (k + 1) // 2) + a = min(a, x + 1) + b = max(b, y + 1) + + return [a, b] + + return dp(firstPlayer, n - secondPlayer + 1, n)