Find Servers That Handled Most Number of Requests LeetCode solution in Python

1
class Solution:
2
def busiestServers(self, k: int, A: List[int], B: List[int]) -> List[int]:
3
available = list(range(k)) # already a min-heap
4
busy = []
5
res = [0] * k
6
for i, a in enumerate(A):
7
while (
8
busy and busy[0][0] <= a
9
): # these are done, put them back as available
10
_, x = heapq.heappop(busy)
11
heapq.heappush(
12
available, i + (x - i) % k
13
) # invariant: min(available) is at least i, at most i+k-1
14
if available:
15
j = heapq.heappop(available) % k
16
heapq.heappush(busy, (a + B[i], j))
17
res[j] += 1
18
a = max(res)
19
return [i for i in range(k) if res[i] == a]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0