Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created September 30, 2014 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanplopes/dfdba66719503011f0c8 to your computer and use it in GitHub Desktop.
Save juanplopes/dfdba66719503011f0c8 to your computer and use it in GitHub Desktop.
Simple scheduling algorithm in python (2nd version)
import heapq
def schedule(tasks):
heap = []
def offer(begin, end):
while heap and heap[0] <= begin:
heapq.heappop(heap)
heapq.heappush(heap, end)
return len(heap)
return ((offer(begin, end), (begin, end)) for begin, end in sorted(tasks))
for room, meeting in schedule([(12,13), (13,15), (17,20), (13,14), (19, 21), (18, 20)]):
print 'Meeting {} should occur in room {}'.format(meeting, room)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment