VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-movie-festival-ii/

⇱ CSES Solutions - Movie Festival II - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Movie Festival II

Last Updated : 23 Mar, 2026

In a movie festival, N movies will be shown. Syrjälä's movie club consists of K members, who will all be attending the festival. Given the starting and ending times of each movie. What is the maximum total number of movies the club members can watch entirely if they act optimally?

Examples:

Input: N = 5, K = 2, movies = {{1, 5}, {8, 10}, {3, 6}, {2, 5}, {6, 9}}
Output: 4
Explanation: The first member can watch 1st movie whose start time is 1 and end time is 5, and 5th movie whose start time is 6 and end time is 9. The second member can watch the movie 4th movie with a start time 2 and end time 4, and 2nd movie with start time 8 and end time 10. In this way total number of movies club members will watch entirely will be 4.

Input: N = 5, K = 1, movies = {{1, 5}, {8, 10}, {3, 6}, {2, 5}, {6, 9}}
Output: 2
Explanation: The first member can watch 1st movie whose start time is 1 and end time is 5 and 5th movie whose start time is 6 and end time is 9.

Greedy Scheduling with Multiset - O(n logk) Time and O(k) Space

  • Sort movies based on start time.
  • Assign the first k movies and store their end times in a multiset.
  • For each remaining movie, if start ≥ minimum end time, reuse that viewer and update the set.
  • Otherwise, if end < maximum end time, replace the worst movie in the set.
  • Maintain count of successfully reassigned movies during iteration.
  • Final answer = assigned count + size of multiset.

Output
4
Comment
Article Tags:
Article Tags: