-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1912. Design Movie Rental System.cpp
More file actions
153 lines (130 loc) · 7.06 KB
/
1912. Design Movie Rental System.cpp
File metadata and controls
153 lines (130 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
1912. Design Movie Rental System
Solved
Hard
Topics
premium lock icon
Companies
Hint
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.
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.
The system should support the following functions:
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.
Rent: Rents an unrented copy of a given movie from a given shop.
Drop: Drops off a previously rented copy of a given movie at a given shop.
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 jth 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.
Implement the MovieRentingSystem class:
MovieRentingSystem(int n, int[][] entries) Initializes the MovieRentingSystem object with n shops and the movies in entries.
List<Integer> search(int movie) Returns a list of shops that have an unrented copy of the given movie as described above.
void rent(int shop, int movie) Rents the given movie from the given shop.
void drop(int shop, int movie) Drops off a previously rented movie at the given shop.
List<List<Integer>> report() Returns a list of cheapest rented movies as described above.
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.
Example 1:
Input
["MovieRentingSystem", "search", "rent", "rent", "report", "drop", "search"]
[[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]]
Output
[null, [1, 0, 2], null, null, [[0, 1], [1, 2]], null, [0, 1]]
Explanation
MovieRentingSystem movieRentingSystem = new MovieRentingSystem(3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]);
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.
movieRentingSystem.rent(0, 1); // Rent movie 1 from shop 0. Unrented movies at shop 0 are now [2,3].
movieRentingSystem.rent(1, 2); // Rent movie 2 from shop 1. Unrented movies at shop 1 are now [1].
movieRentingSystem.report(); // return [[0, 1], [1, 2]]. Movie 1 from shop 0 is cheapest, followed by movie 2 from shop 1.
movieRentingSystem.drop(1, 2); // Drop off movie 2 at shop 1. Unrented movies at shop 1 are now [1,2].
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.
Constraints:
1 <= n <= 3 * 105
1 <= entries.length <= 105
0 <= shopi < n
1 <= moviei, pricei <= 104
Each shop carries at most one copy of a movie moviei.
At most 105 calls in total will be made to search, rent, drop and report.
class MovieRentingSystem {
private static class Node {
final int shop;
final int movie;
final int price;
Node(int shop, int movie, int price) {
this.shop = shop;
this.movie = movie;
this.price = price;
}
}
// Order: price ↑, shop ↑, movie ↑ (strict: never returns 0 for distinct nodes)
private static final Comparator<Node> CMP =
(a, b) -> {
int c = Integer.compare(a.price, b.price);
if (c != 0) return c;
c = Integer.compare(a.shop, b.shop);
if (c != 0) return c;
return Integer.compare(a.movie, b.movie);
};
// Available copies grouped by movie
private final Map<Integer, TreeSet<Node>> availableByMovie = new HashMap<>();
// All currently rented copies
private final TreeSet<Node> rentedSet = new TreeSet<>(CMP);
// Quick lookup from (shop, movie) -> Node
private final Map<Long, Node> byPair = new HashMap<>();
private static long key(int shop, int movie) {
return (((long) shop) << 32) ^ (movie & 0xffffffffL);
}
public MovieRentingSystem(int n, int[][] entries) {
for (int[] e : entries) {
int shop = e[0], movie = e[1], price = e[2];
Node node = new Node(shop, movie, price);
byPair.put(key(shop, movie), node);
availableByMovie
.computeIfAbsent(movie, k -> new TreeSet<>(CMP))
.add(node);
}
}
// Return up to 5 shops with this movie, cheapest then shop asc.
public List<Integer> search(int movie) {
List<Integer> ans = new ArrayList<>(5);
TreeSet<Node> set = availableByMovie.get(movie);
if (set == null || set.isEmpty()) return ans;
Iterator<Node> it = set.iterator();
for (int i = 0; i < 5 && it.hasNext(); i++) {
ans.add(it.next().shop);
}
return ans;
}
// Move (shop,movie) from available -> rented
public void rent(int shop, int movie) {
long k = key(shop, movie);
Node node = byPair.get(k);
if (node == null) return; // defensive
TreeSet<Node> set = availableByMovie.get(movie);
if (set != null) set.remove(node);
rentedSet.add(node);
}
// Move (shop,movie) from rented -> available
public void drop(int shop, int movie) {
long k = key(shop, movie);
Node node = byPair.get(k);
if (node == null) return; // defensive
rentedSet.remove(node);
availableByMovie
.computeIfAbsent(movie, x -> new TreeSet<>(CMP))
.add(node);
}
// Return up to 5 rented copies [shop, movie], cheapest then shop asc, then movie asc.
public List<List<Integer>> report() {
List<List<Integer>> ans = new ArrayList<>(5);
Iterator<Node> it = rentedSet.iterator();
for (int i = 0; i < 5 && it.hasNext(); i++) {
Node n = it.next();
ans.add(Arrays.asList(n.shop, n.movie));
}
return ans;
}
}
/**
* Your MovieRentingSystem object will be instantiated and called as such:
* MovieRentingSystem obj = new MovieRentingSystem(n, entries);
* List<Integer> param_1 = obj.search(movie);
* obj.rent(shop,movie);
* obj.drop(shop,movie);
* List<List<Integer>> param_4 = obj.report();
*/