Find Servers That Handled Most Number of Requests LeetCode solution in Python
1class Solution:2def busiestServers(self, k: int, A: List[int], B: List[int]) -> List[int]:3available = list(range(k)) # already a min-heap4busy = []5res = [0] * k6for i, a in enumerate(A):7while (8busy and busy[0][0] <= a9): # these are done, put them back as available10_, x = heapq.heappop(busy)11heapq.heappush(12available, i + (x - i) % k13) # invariant: min(available) is at least i, at most i+k-114if available:15j = heapq.heappop(available) % k16heapq.heappush(busy, (a + B[i], j))17res[j] += 118a = max(res)19return [i for i in range(k) if res[i] == a]