1912. Design Movie Rental System #2200
-
Topics: You have a movie renting company consisting of Each movie is given as a 2D integer array The system should support the following functions:
Implement the
Note: The test cases will be generated such that Example 1:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to design a movie rental system that supports searching for available movies, renting movies, returning rented movies, and generating a report of the currently rented movies. The system must efficiently handle these operations while ensuring that the results are sorted according to specific criteria. Approach
Let's implement this solution in PHP: 1912. Design Movie Rental System <?php
class MovieRentingSystem {
private $prices;
private $rented;
private $movieHeaps;
private $rentedHeap;
private $inRentedHeap;
/**
* @param Integer $n
* @param Integer[][] $entries
*/
function __construct($n, $entries) {
$this->prices = [];
$this->rented = [];
$this->movieHeaps = [];
$this->rentedHeap = new SplMinHeap();
$this->inRentedHeap = [];
foreach ($entries as $e) {
$shop = $e[0];
$movie = $e[1];
$price = $e[2];
$key = $shop . ',' . $movie;
$this->prices[$key] = $price;
$this->rented[$key] = false;
if (!isset($this->movieHeaps[$movie])) {
$this->movieHeaps[$movie] = new SplMinHeap();
}
$this->movieHeaps[$movie]->insert([$price, $shop]);
$this->inRentedHeap[$key] = false;
}
}
/**
* @param Integer $movie
* @return Integer[]
*/
function search($movie) {
if (!isset($this->movieHeaps[$movie])) {
return [];
}
$heap = $this->movieHeaps[$movie];
$result = [];
$temp = [];
while (count($result) < 5 && $heap->valid()) {
list($price, $shop) = $heap->current();
$heap->next();
$key = $shop . ',' . $movie;
if (!$this->rented[$key]) {
$result[] = $shop;
}
$temp[] = [$price, $shop];
}
foreach ($temp as $item) {
$heap->insert($item);
}
return $result;
}
/**
* @param Integer $shop
* @param Integer $movie
* @return NULL
*/
function rent($shop, $movie) {
$key = $shop . ',' . $movie;
$this->rented[$key] = true;
if (!$this->inRentedHeap[$key]) {
$price = $this->prices[$key];
$this->rentedHeap->insert([$price, $shop, $movie]);
$this->inRentedHeap[$key] = true;
}
}
/**
* @param Integer $shop
* @param Integer $movie
* @return NULL
*/
function drop($shop, $movie) {
$key = $shop . ',' . $movie;
$this->rented[$key] = false;
}
/**
* @return Integer[][]
*/
function report() {
$result = [];
$temp = [];
while (count($result) < 5 && $this->rentedHeap->valid()) {
list($price, $shop, $movie) = $this->rentedHeap->current();
$this->rentedHeap->next();
$key = $shop . ',' . $movie;
if ($this->rented[$key]) {
$result[] = [$shop, $movie];
$temp[] = [$price, $shop, $movie];
}
$this->inRentedHeap[$key] = false;
}
foreach ($temp as $item) {
list($price, $shop, $movie) = $item;
$this->rentedHeap->insert([$price, $shop, $movie]);
$key = $shop . ',' . $movie;
$this->inRentedHeap[$key] = true;
}
return $result;
}
}
/**
* Your MovieRentingSystem object will be instantiated and called as such:
* $obj = MovieRentingSystem($n, $entries);
* $ret_1 = $obj->search($movie);
* $obj->rent($shop, $movie);
* $obj->drop($shop, $movie);
* $ret_4 = $obj->report();
*/
// Test cases
$movieRentingSystem = new MovieRentingSystem(3, [
[0, 1, 5], [0, 2, 6], [0, 3, 7],
[1, 1, 4], [1, 2, 7], [2, 1, 5]
]);
print_r($movieRentingSystem->search(1));
// [1, 0, 2]
$movieRentingSystem->rent(0, 1);
$movieRentingSystem->rent(1, 2);
print_r($movieRentingSystem->report());
// [[0, 1], [1, 2]]
$movieRentingSystem->drop(1, 2);
print_r($movieRentingSystem->search(2));
// [0, 1]
?> Explanation:
This approach efficiently manages the movie rental operations by leveraging min-heaps and lazy deletion, ensuring that the system meets the required performance constraints. |
Beta Was this translation helpful? Give feedback.
We need to design a movie rental system that supports searching for available movies, renting movies, returning rented movies, and generating a report of the currently rented movies. The system must efficiently handle these operations while ensuring that the results are sorted according to specific criteria.
Approach
Initialization:
prices
).rented
).movieHeaps
) that contains all copies (as[price, shop]
pairs) available for that movie. This heap helps in efficiently retrieving the cheapest available shops during search …