![]() |
VOOZH | about |
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: 3
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: 2
Below is the implementation of the above approach:
3
Time Complexity: O(MAX)
Auxiliary Space: O(MAX), where MAX is 100001.
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.
Below is the implementation of the above approach:
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.