Facebook
From Insensitive Tern, 4 Years ago, written in Plain Text.
This paste is a reply to Re: Untitled from Ample Pelican - view diff
Embed
Download Paste or View Raw
Hits: 230
  1. def getAvgWaitTime(n, cust):
  2.     heap = []
  3.     curTime = 0
  4.     waitTime = 0
  5.     cust = sorted(cust)
  6.     i = 0
  7.  
  8.     while i < n:
  9.         if curTime < cust[i][0]:
  10.             curTime = cust[i][0]
  11.         while i < n and (curTime >= cust[i][0]):
  12.             heapq.heappush(heap, (cust[i][1], cust[i][0]))
  13.             i += 1
  14.  
  15.         while (i < n) and curTime < cust[i][0] and len(heap) > 0:
  16.             time, wait = calculateWaiting(heap, curTime)
  17.             curTime += time
  18.             waitTime += wait
  19.  
  20.     # Clear all the jobs
  21.     while len(heap) > 0:
  22.         time, wait = calculateWaiting(heap, curTime)
  23.         curTime += time
  24.         waitTime += wait
  25.  
  26.     return waitTime / n
  27.  
  28.  
  29. def calculateWaiting(heap, curTime):
  30.     wait = 0
  31.     cur = heapq.heappop(heap)
  32.     wait = curTime - cur[1] + cur[0]
  33.     return (cur[0], wait)
  34.  
  35.  
  36. n = int(input().strip())
  37. cust = []
  38. for i in range(n):
  39.     ar = map(int, input().strip().split(' '))
  40.     cust.append((ar[0], ar[1]))
  41.  
  42.  
  43. result = getAvgWaitTime(n, cust)
  44. print(result)

Replies to Re: Re: Untitled rss

Title Name Language When
Re: Re: Re: Untitled Gray Wigeon python 4 Years ago.