educative.io

Do we need minHeap? Seems only tracking the minimum of end time works for me

Only tracking the minimum of end time seems work for me.
Any ideas of what I might miss?

def min_meeting_rooms(meetings):

meetings.sort(key = lambda x: x.start)

rooms = 1

min_end_time = meetings[0].end

for i in range(1, len(meetings)):

if meetings[i - 1].end > meetings[i].start:

  if min_end_time > meetings[i].start:

    rooms += 1

min_end_time = min(min_end_time, meetings[i].end)

return rooms

How about this conditional statement instead of the two nested “ifs”

if meetings[i - 1].end > meetings[i].start and min_end_time > meetings[i].start:

you don’t need to have a nested if as you are not defining a new index or a counter after the first ‘if’. I had a very long solution but your approach is very straightforward and deadly simple.


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Problem Challenge 1 - Grokking the Coding Interview: Patterns for Coding Questions