3

There are n number of conference rooms available in a company for meetings. You need to book a meeting for a particular time slot. Write an algorithm to determine the number of conference rooms available for the meeting with given start time and end time.

Hint: any conference room with non- overlapping meeting will be selected.

I was thinking segment trees can provide O(logN) for this, but not sure thats the best approach

OmG
  • 18,337
  • 10
  • 57
  • 90

1 Answers1

1

Yes, O(logN) per query is optimal.

If you have to schedule all the meetings, you can put all the start times and end times into a vector, sort them then traverse the sorted vector and assigning meeting rooms. Still O(NlogN) due to sorting but the constant is smaller and memory access patterns should be better than segment trees (that is faster if the data is large enough).

Sorin
  • 11,863
  • 22
  • 26
  • Can you give further details into how the sorting would be made? would you sort based on start time or ending time? I know that segment tree search is O(LogN) but building tree might be slow O(Nlogn) –  May 19 '16 at 19:26
  • Say you have [10-12] and [11-13] meetings. I create a single vector with [(10, start), (12, end), (11, start), (13, end)] and sort it by the hour to get [(10, start), (11, start), (12, end), (13, end)]. Now if you go throught it you can increment number of rooms in use when you see something with 'start' and decrease it when you have something with 'end'. You just need to figure out the maximum over the various query intervals (a stack would help). Overall you are still O(NlogN) because of the sorting. – Sorin May 20 '16 at 12:04
  • Can we use interval tree for this problem statement? – shan kulkarni May 01 '17 at 14:10