VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-halls-required-for-class-scheduling/

⇱ Minimum halls required for class scheduling - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum halls required for class scheduling

Last Updated : 1 Feb, 2024

Given N lecture timings, with their start time and end time (both inclusive), the task is to find the minimum number of halls required to hold all the classes such that a single hall can be used for only one lecture at a given time. Note that the maximum end time can be 105.
Examples:

Input: lectures[][] = {{0, 5}, {1, 2}, {1, 10}} 
Output:
All lectures must be held in different halls because 
at time instance 1 all lectures are ongoing.
Input: lectures[][] = {{0, 5}, {1, 2}, {6, 10}} 
Output:

Approach:

  • Assuming that time T starts with 0. The task is to find the maximum number of lectures that are ongoing at a particular instance of time. This will give the minimum number of halls required to schedule all the lectures.
  • To find the number of lectures ongoing at any instance of time. Maintain a prefix_sum[] which will store the number of lectures ongoing at any instance of time t. For any lecture with timings between [s, t], do prefix_sum[s]++ and prefix_sum[t + 1]--.
  • Afterward, the cumulative sum of this prefix array will give the count of lectures going on at any instance of time.
  • The maximum value for any time instant t in the array is the minimum number of halls required.


Below is the implementation of the above approach: 


Output
3

Time Complexity: O(MAX)
Auxiliary Space: O(MAX), where MAX is 100001.

Another Approach:

The above approach works when MAX is limited to 105. When the limits of MAX are extended up to 109, we cannot use above approach due to Memory Limit and Time Limit Constraints.

So, we think in a different dimension, towards sorting and cumulative sum. Store the lecture time (start time and end time) in chronological order, +1 denoting the start time of a lecture and -1 denoting the end time of a lecture. Then apply the concept of cumulative sum, this gives the maximum number of lectures being conducted at a time. This gives the bare minimum number of halls that are required.

Algorithm:

  • Initialize a vector of pair, Time, the first value of which indicates the entry or exit time of lecture and the second value denotes whether the lecture starts or ends. 
  • Traverse the lectures vector and store the values in the vector Time.
  • Sort the vector Time.
  • Maintain two variables answer = 0, and sum = 0. answer denotes the final answer to be returned and sum denotes the number of lectures going on at a particular time.
  • Traverse the Time vector, add the second value of the pair into sum and update the answer variable.
  • Return answer.

Below is the implementation of the above approach: 


Output
3

Time Complexity: O(n*log(n))
Auxiliary Space: O(n)

Note that instead of vector of pair, one can use map or priority queue, the time complexity and space complexity would remain the same. 

Comment