We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I always prefer to give variables more meaningful names, and provide some comments. In my solution, currently_waiting is a heap of customers that already placed their orders, updated every loop.
importheapqdefminimumAverage(customers):# customers = [(order_time, cooking_time)]customers.sort()# Sort by order time, then cooking timenumber_of_customers=len(customers)current_time=total_wait=0currently_waiting=[]whilecustomersorcurrently_waiting:# Move to a heap of currently-waiting customers (as of `current_time`)whilecustomersandcustomers[0][0]<=current_time:order_time,cooking_time=customers.pop(0)heapq.heappush(currently_waiting,(cooking_time,order_time))ifnotcurrently_waiting:# no orders yet, so fast forward to next customercurrent_time=customers[0][0]continue# Cook the order that will finish the sooner (first item on the heap)cooking_time,order_time=heapq.heappop(currently_waiting)total_wait+=current_time+cooking_time-order_timecurrent_time+=cooking_timereturntotal_wait//number_of_customers
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Average Waiting Time
You are viewing a single comment's thread. Return to all comments →
I always prefer to give variables more meaningful names, and provide some comments. In my solution,
currently_waiting
is a heap of customers that already placed their orders, updated every loop.