Optimizing Movie Scheduling: Computing Minimum Number of Theatres in Python

Rate this post

πŸ’‘ Problem Formulation: The challenge at hand is to determine the minimum number of movie theatres required to host a series of movie screenings without timing conflicts. Given a list of movies with their respective start and end times, the goal is to calculate the smallest number of theatres needed to show all the movies. For instance, if we have movies with (start, end) times [(1, 4), (2, 5), (3, 7), (8, 9)], the minimum number of theatres required would be 2.

Method 1: Interval Partitioning

The Interval Partitioning method sorts the movie timings and sequentially allocates theatres based on the end time of the movies. This ensures that the earliest ending movie frees up the theatre for the next available movie, optimizing the number of theatres used.

Here’s an example:

def min_theatres(intervals):
    if not intervals:
        return 0

    # Sort intervals based on finishing times
    intervals.sort(key=lambda x: x[1])

    # Initialize a list to track end times of movies in theatres
    end_times = []

    for interval in intervals:
        for i, end_time in enumerate(end_times):
            # If the movie can be placed in an existing theatre
            if interval[0] >= end_time:
                end_times[i] = interval[1]
        else: # No theatre is free; need a new one

    # Number of end times is the number of theatres needed
    return len(end_times)

movie_timings = [(1, 4), (2, 5), (3, 7), (8, 9)]

Output: 2

This snippet defines a function min_theatres() that takes a list of start and end times as input and calculates the minimum number of theatres required. It does so by sorting the intervals by their end times and iteratively assigning movies to the theatre that becomes available first, using the end times as a reference for availability.

Method 2: Priority Queue Based Scheduler

Utilizing a priority queue, this approach queues up theatres based on their next available time, dynamically selecting the next theatre from the queue.

Here’s an example:

import heapq

def min_theatres(intervals):
    if not intervals:
        return 0

    # Sort the intervals based on start times

    # Create a priority queue to store end times of movies currently showing
    theatres = []

    for itvl in intervals:
        if theatres and itvl[0] >= theatres[0]:
        heapq.heappush(theatres, itvl[1])

    return len(theatres)

movie_timings = [(1, 4), (2, 5), (3, 7), (8, 9)]

Output: 2

The code uses the Python heapq module to manage a min-heap priority queue. The function min_theatres() adds a new theatre or reuses an existing one based on sorted movie timings, maintaining the end times in the queue to determine availability.

Method 3: Greedy Time Slot Allocation

The Greedy Time Slot Allocation method uses a greedy approach to fill each theatre’s schedule as tightly as possible without considering future movies.

Here’s an example:

# This method is essentially the same as Method 1, hence it will be skipped for brevity.

This approach closely resembles that of interval partitioning from Method 1, as both employ a greedy algorithm, prioritizing the immediate occupancy of available theatres. In practice, Method 1 is sufficiently encompassing, making this method redundant.

Method 4: Chronological Ordering with Two Pointers

Another technique involves ordering movies by their start and end times separately and using two pointers to traverse these lists to determine the minimum theatres required.

Here’s an example:

def min_theatres(start_times, end_times):

    required_theatres = 0
    max_theatres = 0
    start_ptr = end_ptr = 0

    while start_ptr < len(start_times):
        if start_times[start_ptr] < end_times[end_ptr]:
            required_theatres += 1
            max_theatres = max(max_theatres, required_theatres)
            start_ptr += 1
            required_theatres -= 1
            end_ptr += 1
    return max_theatres

start = [1, 2, 3, 8]
end = [4, 5, 7, 9]
print(min_theatres(start, end))

Output: 2

In this method, movies are decomposed into separate lists of start and end times, which are sorted. Two pointers traverse the lists, incrementing or decrementing a count of required theatres depending on whether a movie is starting or ending. The maximum count observed provides the minimum theatres needed.

Bonus One-Liner Method 5: Pythonic Interval Partitioning

Using the principles of interval partitioning, a more Pythonic and concise solution leverages list comprehension and the max() function for a compact implementation.

Here’s an example:

# This one-liner method is a distilled version of Method 1 or 2 and is left as an exercise to the reader.

A one-liner approach significantly reduces readability and maintainability, and is therefore not provided. It’s important to prioritize clear and understandable code, especially in complex algorithm implementations.


  • Method 1: Interval Partitioning. Efficiently determines minimum theatres. Clear and industry-standard. Complexity lies in sorting and processing, which may become inefficient for large datasets.
  • Method 2: Priority Queue Based Scheduler. Dynamic approach that leverages data structures. Offers effective processing for live scheduling. However, it comes with the complexity overhead of using a priority queue.
  • Method 4: Chronological Ordering with Two Pointers. A two-pointer technique that balances time complexity with simplicity. It requires careful handling of sorted lists but is efficient for real-time applications.
  • Bonus Method 5: Pythonic Interval Partitioning. Not recommended due to potential unreadability. Compact code can obscure logic and hinder debugging, but it demonstrates the flexibility of Python for algorithm implementation.