Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum number of movie theatres required to show all movies in python
Suppose we have a list of intervals for different movie showings (they can be overlapped), we have to find the minimum number of theatres required to be able to show all of the movies.
So, if the input is like intervals = [[20, 65],[0, 40],[50, 140]], then the output will be 2, as [20, 65] and [0, 40] are overlapping. [20, 65] and [50, 140] are also overlapping but [0, 40] and [50, 140] are not. So we need 2 theaters.
Algorithm
To solve this, we will follow these steps ?
- Create a new list to store time events
- for each interval [a, b] in intervals, do
- insert [a, 1] at the end of events list (movie starts)
- insert [b, −1] at the end of events list (movie ends)
- Initialize answer := 0, count := 0
- for each pair (time, delta) in events list in sorted form, do
- count := count + delta
- answer := maximum of answer and count
- return answer
Example
Let us see the following implementation to get better understanding ?
class Solution:
def solve(self, intervals):
events = []
# Create events for start (+1) and end (-1) times
for start, end in intervals:
events.append((start, 1)) # Movie starts
events.append((end, -1)) # Movie ends
# Sort events by time
events.sort()
max_theatres = 0
current_movies = 0
# Process events chronologically
for time, delta in events:
current_movies += delta
max_theatres = max(max_theatres, current_movies)
return max_theatres
# Test the solution
ob = Solution()
intervals = [[20, 65], [0, 40], [50, 140]]
result = ob.solve(intervals)
print(f"Minimum theatres required: {result}")
The output of the above code is ?
Minimum theatres required: 2
How It Works
The algorithm uses an event-based approach ?
- Create Events: Each movie interval creates two events − start time (+1) and end time (−1)
- Sort Events: Process events chronologically to track overlapping movies
- Count Active Movies: At each time point, increment for starts and decrement for ends
- Track Maximum: The peak number of concurrent movies equals minimum theatres needed
Step-by-step Trace
For intervals [[20, 65], [0, 40], [50, 140]] ?
intervals = [[20, 65], [0, 40], [50, 140]]
# Events created:
events = [(0, 1), (20, 1), (40, -1), (50, 1), (65, -1), (140, -1)]
# After sorting (already sorted):
print("Time | Delta | Active Movies | Max Theatres")
print("-----|-------|---------------|-------------")
current = 0
max_theatres = 0
for time, delta in events:
current += delta
max_theatres = max(max_theatres, current)
print(f"{time:4d} | {delta:5d} | {current:13d} | {max_theatres:11d}")
The output shows the tracking process ?
Time | Delta | Active Movies | Max Theatres -----|-------|---------------|------------- 0 | 1 | 1 | 1 20 | 1 | 2 | 2 40 | -1 | 1 | 2 50 | 1 | 2 | 2 65 | -1 | 1 | 2 140 | -1 | 0 | 2
Conclusion
The event-based algorithm efficiently finds the minimum theatres needed by tracking overlapping intervals. The maximum number of concurrent movies at any time determines the answer.
