Last active
August 29, 2015 14:19
-
-
Save schmohlio/fe200a77628e28355bb4 to your computer and use it in GitHub Desktop.
finding max number of meeting rooms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
meetings_sample = [(0, 30), (60, 90), (20, 65)] | |
[(0,True),(30,False),(60,True),(90,False),(20,True),(65,False)] | |
def count_rooms(meeting_events): | |
meeting_events.sort(lambda x,y: x[0]<y[0]) # O(nlogn), need to add sorting here so that False is first | |
# not sure if that sort is left associative or right | |
# associative | |
ongoing_meetings = [] # pretend stack | |
max_num_meetings = 0 # initialize | |
stack_size = 0 | |
for time, is_start in meeting_events | |
if (is_start): | |
ongoing_meetings.append((time)) | |
stack_size += 1 | |
else: | |
ongoing_meetings.pop() | |
stack_size -= 1 | |
max_num_meetings = max(max_num_meetings, stack_size) | |
return max_num_meetings | |
# return True if overlap in meetings | |
def has_overlap(meetings): | |
meetings.sort(key= lambda x: x[0]) # O(nlogn) | |
prev_end_time = -1 | |
for start, end in meetings: | |
if start < prev_end_time: | |
return true | |
else: | |
prev_end_time = end | |
return false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the count_rooms() at line 38 might be more efficient due to O(1) additional space, since it doesnt have stack?