Skip to content

Commit 6c6ebd6

Browse files
committed
1 parent c884af4 commit 6c6ebd6

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1441,3 +1441,4 @@ mod s1906_minimum_absolute_difference_queries;
14411441
mod s1909_remove_one_element_to_make_the_array_strictly_increasing;
14421442
mod s1910_remove_all_occurrences_of_a_substring;
14431443
mod s1911_maximum_alternating_subsequence_sum;
1444+
mod s1912_design_movie_rental_system;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* [1912] Design Movie Rental System
3+
* You have a movie renting company consisting of n shops. You want to implement a renting system that supports searching for, booking, and returning movies. The system should also support generating a report of the currently rented movies.
4+
* Each movie is given as a 2D integer array entries where entries[i] = [shopi, moviei, pricei] indicates that there is a copy of movie moviei at shop shopi with a rental price of pricei. Each shop carries at most one copy of a movie moviei.
5+
* The system should support the following functions:
6+
*
7+
* Search: Finds the cheapest 5 shops that have an unrented copy of a given movie. The shops should be sorted by price in ascending order, and in case of a tie, the one with the smaller shopi should appear first. If there are less than 5 matching shops, then all of them should be returned. If no shop has an unrented copy, then an empty list should be returned.
8+
* Rent: Rents an unrented copy of a given movie from a given shop.
9+
* Drop: Drops off a previously rented copy of a given movie at a given shop.
10+
* Report: Returns the cheapest 5 rented movies (possibly of the same movie ID) as a 2D list res where res[j] = [shopj, moviej] describes that the j^th cheapest rented movie moviej was rented from the shop shopj. The movies in res should be sorted by price in ascending order, and in case of a tie, the one with the smaller shopj should appear first, and if there is still tie, the one with the smaller moviej should appear first. If there are fewer than 5 rented movies, then all of them should be returned. If no movies are currently being rented, then an empty list should be returned.
11+
*
12+
* Implement the MovieRentingSystem class:
13+
*
14+
* MovieRentingSystem(int n, int[][] entries) Initializes the MovieRentingSystem object with n shops and the movies in entries.
15+
* List<Integer> search(int movie) Returns a list of shops that have an unrented copy of the given movie as described above.
16+
* void rent(int shop, int movie) Rents the given movie from the given shop.
17+
* void drop(int shop, int movie) Drops off a previously rented movie at the given shop.
18+
* List<List<Integer>> report() Returns a list of cheapest rented movies as described above.
19+
*
20+
* Note: The test cases will be generated such that rent will only be called if the shop has an unrented copy of the movie, and drop will only be called if the shop had previously rented out the movie.
21+
*
22+
* Example 1:
23+
*
24+
* Input
25+
* ["MovieRentingSystem", "search", "rent", "rent", "report", "drop", "search"]
26+
* [[3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]], [1], [0, 1], [1, 2], [], [1, 2], [2]]
27+
* Output
28+
* [null, [1, 0, 2], null, null, [[0, 1], [1, 2]], null, [0, 1]]
29+
* Explanation
30+
* MovieRentingSystem movieRentingSystem = new MovieRentingSystem(3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]);
31+
* movieRentingSystem.search(1); // return [1, 0, 2], Movies of ID 1 are unrented at shops 1, 0, and 2. Shop 1 is cheapest; shop 0 and 2 are the same price, so order by shop number.
32+
* movieRentingSystem.rent(0, 1); // Rent movie 1 from shop 0. Unrented movies at shop 0 are now [2,3].
33+
* movieRentingSystem.rent(1, 2); // Rent movie 2 from shop 1. Unrented movies at shop 1 are now [1].
34+
* movieRentingSystem.report(); // return [[0, 1], [1, 2]]. Movie 1 from shop 0 is cheapest, followed by movie 2 from shop 1.
35+
* movieRentingSystem.drop(1, 2); // Drop off movie 2 at shop 1. Unrented movies at shop 1 are now [1,2].
36+
* movieRentingSystem.search(2); // return [0, 1]. Movies of ID 2 are unrented at shops 0 and 1. Shop 0 is cheapest, followed by shop 1.
37+
*
38+
*
39+
* Constraints:
40+
*
41+
* 1 <= n <= 3 * 10^5
42+
* 1 <= entries.length <= 10^5
43+
* 0 <= shopi < n
44+
* 1 <= moviei, pricei <= 10^4
45+
* Each shop carries at most one copy of a movie moviei.
46+
* At most 10^5 calls in total will be made to search, rent, drop and report.
47+
*
48+
*/
49+
pub struct Solution {}
50+
51+
// problem: https://leetcode.com/problems/design-movie-rental-system/
52+
// discuss: https://leetcode.com/problems/design-movie-rental-system/discuss/?currentPage=1&orderBy=most_votes&query=
53+
54+
// submission codes start here
55+
56+
// Credit: https://leetcode.com/problems/design-movie-rental-system/solutions/3239007/just-a-runnable-solution/
57+
58+
#[derive(Default)]
59+
struct MovieRentingSystem {
60+
price: std::collections::BTreeMap<(i32, i32), i32>,
61+
unrented: std::collections::HashMap<i32, std::collections::BTreeSet<(i32, i32)>>,
62+
rented: std::collections::BTreeSet<(i32, i32, i32)>,
63+
}
64+
65+
/**
66+
* `&self` means the method takes an immutable reference.
67+
* If you need a mutable reference, change it to `&mut self` instead.
68+
*/
69+
impl MovieRentingSystem {
70+
fn new(n: i32, entries: Vec<Vec<i32>>) -> Self {
71+
let mut price = std::collections::BTreeMap::new();
72+
let mut unrented =
73+
std::collections::HashMap::<i32, std::collections::BTreeSet<(i32, i32)>>::new();
74+
for e in entries {
75+
let shop = e[0];
76+
let movie = e[1];
77+
let p = e[2];
78+
price.insert((shop, movie), p);
79+
unrented.entry(movie).or_default().insert((p, shop));
80+
}
81+
Self {
82+
price,
83+
unrented,
84+
rented: std::collections::BTreeSet::new(),
85+
}
86+
}
87+
88+
fn search(&mut self, movie: i32) -> Vec<i32> {
89+
let mut ans = Vec::new();
90+
if let Some(s) = self.unrented.get(&movie) {
91+
for (_, shop) in s.iter().take(5) {
92+
ans.push(*shop);
93+
}
94+
}
95+
ans
96+
}
97+
98+
fn rent(&mut self, shop: i32, movie: i32) {
99+
let p = self.price[&(shop, movie)];
100+
self.unrented.get_mut(&movie).unwrap().remove(&(p, shop));
101+
self.rented.insert((p, shop, movie));
102+
}
103+
104+
fn drop(&mut self, shop: i32, movie: i32) {
105+
let p = self.price[&(shop, movie)];
106+
self.rented.remove(&(p, shop, movie));
107+
self.unrented.get_mut(&movie).unwrap().insert((p, shop));
108+
}
109+
110+
fn report(&mut self) -> Vec<Vec<i32>> {
111+
let mut ans = Vec::new();
112+
for (_, shop, movie) in self.rented.iter().take(5) {
113+
ans.push(vec![*shop, *movie]);
114+
}
115+
ans
116+
}
117+
}
118+
119+
/**
120+
* Your MovieRentingSystem object will be instantiated and called as such:
121+
* let obj = MovieRentingSystem::new(n, entries);
122+
* let ret_1: Vec<i32> = obj.search(movie);
123+
* obj.rent(shop, movie);
124+
* obj.drop(shop, movie);
125+
* let ret_4: Vec<Vec<i32>> = obj.report();
126+
*/
127+
128+
// submission codes end
129+
130+
#[cfg(test)]
131+
mod tests {
132+
use super::*;
133+
134+
#[test]
135+
fn test_1912_example_1() {
136+
let mut movie_renting_system = MovieRentingSystem::new(
137+
3,
138+
vec![
139+
vec![0, 1, 5],
140+
vec![0, 2, 6],
141+
vec![0, 3, 7],
142+
vec![1, 1, 4],
143+
vec![1, 2, 7],
144+
vec![2, 1, 5],
145+
],
146+
);
147+
assert_eq!(movie_renting_system.search(1), vec![1, 0, 2]); // return [1, 0, 2], Movies of ID 1 are unrented at shops 1, 0, and 2. Shop 1 is cheapest; shop 0 and 2 are the same price, so order by shop number.
148+
movie_renting_system.rent(0, 1); // Rent movie 1 from shop 0. Unrented movies at shop 0 are now [2,3].
149+
movie_renting_system.rent(1, 2); // Rent movie 2 from shop 1. Unrented movies at shop 1 are now [1].
150+
assert_eq!(movie_renting_system.report(), vec![vec![0, 1], vec![1, 2]]); // return [[0, 1], [1, 2]]. Movie 1 from shop 0 is cheapest, followed by movie 2 from shop 1.Z
151+
movie_renting_system.drop(1, 2); // Drop off movie 2 at shop 1. Unrented movies at shop 1 are now [1,2].
152+
assert_eq!(movie_renting_system.search(2), vec![0, 1]); // return [0, 1]. Movies of ID 2 are unrented at shops 0 and 1. Shop 0 is cheapest, followed by shop 1.
153+
}
154+
}

0 commit comments

Comments
 (0)