Advertisement

Design Movie Rental System - LeetCode 1912 Solution

Design Movie Rental System - Complete Solution Guide

Design Movie Rental System is LeetCode problem 1912, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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] = [shop i , movie i , price i ] indicates that there is a copy of movie movie i at shop shop i with a rental price of price i . Each shop carries at most one copy of a movie movie i . The system

Detailed Explanation

The problem requires designing a movie rental system with functionalities to search for the cheapest available movies in different shops, rent movies, return movies (drop), and report the cheapest rented movies. The system must efficiently manage the availability and rental status of movies across multiple shops. The system must handle ties in prices by prioritizing shops with smaller shop IDs and, for reporting, prioritizing also by movie ID. The number of results returned by search and report should be capped at 5.

Solution Approach

The solution uses HashMaps and Priority Queues (or Sets) to efficiently manage the movie rental system. A HashMap `unrented` stores the available movies at each shop, organized by movie ID. A Set `rented` stores the currently rented movies. A HashMap `prices` stores the price of each movie at each shop for quick access. The `search` function uses a priority queue to find the 5 cheapest shops with the given movie. The `rent` function removes the movie from the `unrented` HashMap and adds it to the `rented` Set. The `drop` function does the opposite. The `report` function uses a priority queue to find the 5 cheapest rented movies.

Step-by-Step Algorithm

  1. Step 1: Initialization: Create HashMaps `unrented` (movie ID -> set of (price, shop)), `prices` ((shop, movie) -> price), and a Set `rented` ((price, shop, movie)). Populate these data structures based on the input `entries`.
  2. Step 2: Search: Given a movie ID, check if it exists in the `unrented` HashMap. If not, return an empty list. Otherwise, use `heapq.nsmallest()` (or a PriorityQueue) to find the 5 cheapest (price, shop) tuples in the set associated with the movie ID in the `unrented` HashMap. Extract the shop IDs from these tuples and return them as a list.
  3. Step 3: Rent: Given a shop and movie ID, retrieve the price from the `prices` HashMap. Remove the (price, shop) tuple from the set associated with the movie ID in the `unrented` HashMap. Add the (price, shop, movie) tuple to the `rented` Set.
  4. Step 4: Drop: Given a shop and movie ID, retrieve the price from the `prices` HashMap. Remove the (price, shop, movie) tuple from the `rented` Set. Add the (price, shop) tuple to the set associated with the movie ID in the `unrented` HashMap.
  5. Step 5: Report: Use `heapq.nsmallest()` (or a PriorityQueue) to find the 5 cheapest (price, shop, movie) tuples in the `rented` Set. Extract the (shop, movie) pairs from these tuples and return them as a list of lists.

Key Insights

  • Insight 1: Using appropriate data structures like HashMaps and Priority Queues (or Sets) are crucial for efficient search, insertion, and deletion operations.
  • Insight 2: Maintaining separate data structures for unrented and rented movies simplifies the implementation of the search, rent, and drop functionalities.
  • Insight 3: Utilizing the `heapq.nsmallest()` or a Priority Queue with a size limit can efficiently find the top 5 cheapest movies or shops, avoiding full sorting.
  • Insight 4: The key for price lookups should be a combination of (shop,movie) as that defines a unique entry with an associated price.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Design, Heap (Priority Queue), Ordered Set.

Companies

Asked at: Flipkart.