Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. def satisfying_booking(R):
  2. '''
  3. Input: R | Tuple of |R| talk request tuples (s, t)
  4. Output: B | Tuple of room booking triples (k, s, t)
  5. | that is the booking schedule that satisfies R
  6. '''
  7. B = []
  8. times = []
  9. for r in R:
  10. times.append((r[0],1))
  11. times.append((r[1],-1))
  12. times.sort()
  13.  
  14. bookings = []
  15. rooms = 0
  16. current_time = times[0][0]
  17. prev_time = times[0][0]
  18. for i in range(len(times)):
  19. current_time = times[i][0]
  20. if current_time == prev_time:
  21. rooms += times[i][1]
  22. elif current_time != prev_time:
  23. bookings.append((prev_time, rooms))
  24. rooms += times[i][1]
  25. prev_time = current_time
  26.  
  27. bookings.append((times[-1][0], rooms))
  28.  
  29. previous_number_of_rooms = 1
  30. number_of_rooms = 1
  31. start_time = None
  32.  
  33. for bk in bookings:
  34. if start_time == None:
  35. start_time = bk[0]
  36. number_of_rooms = bk[1]
  37. if number_of_rooms != previous_number_of_rooms:
  38. if previous_number_of_rooms != 0:
  39. B.append((previous_number_of_rooms,start_time, bk[0]))
  40. start_time = bk[0]
  41. previous_number_of_rooms = number_of_rooms
  42.  
  43. return B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement