Golang solutions using three heaps [with runnable tests]
Idea is based on the following Python solution: https://leetcode.com/problems/find-servers-that-handled-most-number-of-requests/discuss/876883/Python-using-only
Idea is based on the following Python solution: https://leetcode.com/problems/find-servers-that-handled-most-number-of-requests/discuss/876883/Python-using-only-heaps\n\nRunnable tests on playground: https://play.golang.org/p/KmXY1RIWA5z\n\n```go\nfunc busiestServers(k int, arrival []int, load []int) []int {\n\t// min heaps of items, sorted by "priority" value:\n\t//  - jobs heap    - will store requests that are being processed right now\n\t//  - serversBef   - will store ordered list of servers with server number less than one we need\n\t//  - serversAfter - will store ordered list of servers with server number equal or greater\n\tvar jobs, serversBef, serversAfter PriorityQueue\n\tvar reqs = make([]int, k) // to store number of handled requests\n\n\t// populate initial list of available servers\n\tfor i := 0; i < k; i++ {\n\t\tvar node = &Item{\n\t\t\tnum:      i,\n\t\t\tpriority: i,\n\t\t\tindex:    i,\n\t\t}\n\n\t\tserversBef = append(serversBef, node)\n\t}\n\theap.Init(&serversBef)\n\n\tfor i := range arrival {\n\t\tsec := arrival[i]\n\t\tload := load[i]\n\t\tnum := i % k\n\t\t// fmt.Println(sec, "Looking for server", num)\n\n\t\t// if we need first server, than swap\n\t\t// "before" and "after" heaps, because all\n\t\t// servers should become "after" at this point\n\t\tif num == 0 {\n\t\t\tserversAfter = serversBef\n\t\t\tserversBef = PriorityQueue{}\n\t\t}\n\n\t\t// check if some jobs completed, so we can\n\t\t// count server as available\n\t\tfor jobs.Len() > 0 {\n\t\t\t// since jobs are prioritized (sorted) by\n\t\t\t// time, it is enough to keep popping jobs\n\t\t\t// from the queue and check if time it\'s time\n\t\t\tjob := heap.Pop(&jobs).(*Item)\n\t\t\tif job.priority > sec {\n\t\t\t\t// if job\'s time hasn\'t passed yet\n\t\t\t\t// put it back to the list, and stop checking\n\t\t\t\theap.Push(&jobs, job)\n\t\t\t\tbreak\n\t\t\t}\n\n\t\t\t// now we know that this server become available\n\t\t\tnode := &Item{num: job.num, priority: job.num}\n\t\t\t// put it in the proper queue of nodes (sorted by server num)\n\t\t\tif job.num < num {\n\t\t\t\theap.Push(&serversBef, node)\n\t\t\t} else {\n\t\t\t\theap.Push(&serversAfter, node)\n\t\t\t}\n\t\t}\n\n\t\tvar node *Item\n\t\t// check if any server with number equal or\n\t\t// greater to our is available, if it is\n\t\t// pop the smallest one, if not\n\t\t// pop from the "before" queue\n\t\tif serversAfter.Len() > 0 {\n\t\t\tnode = heap.Pop(&serversAfter).(*Item)\n\t\t} else if serversBef.Len() > 0 {\n\t\t\tnode = heap.Pop(&serversBef).(*Item)\n\t\t} else {\n\t\t\t// if both queues are empty, this means\n\t\t\t// that all servers are busy at the moment\n\t\t\tcontinue\n\t\t}\n\n\t\t// add job processed by this node to the\n\t\t// jobs queue\n\t\tjob := &Item{\n\t\t\tnum:      node.num,\n\t\t\tpriority: sec + load,\n\t\t}\n\t\theap.Push(&jobs, job)\n\t\t// and increase processes requests count\n\t\treqs[node.num]++\n\t}\n\n\t// now find the busiest servers\n\tvar mr = map[int][]int{}\n\tvar max = 0\n\tfor i, r := range reqs {\n\t\tif r > max {\n\t\t\tmax = r\n\t\t}\n\t\tmr[r] = append(mr[r], i)\n\t}\n\n\treturn mr[max]\n}\n\n// PQ implementation:\n\n// https://golang.org/src/container/heap/example_pq_test.go\n// An Item is something we manage in a priority queue.\ntype Item struct {\n\tnum      int\n\tpriority int\n\tindex    int\n}\n\n// A PriorityQueue implements heap.Interface and holds Items.\ntype PriorityQueue []*Item\n\nfunc (pq PriorityQueue) Len() int { return len(pq) }\n\nfunc (pq PriorityQueue) Less(i, j int) bool {\n\tif pq[i].priority == pq[j].priority {\n\t\treturn pq[i].num < pq[j].num\n\t}\n\treturn pq[i].priority < pq[j].priority\n}\n\nfunc (pq PriorityQueue) Swap(i, j int) {\n\tpq[i], pq[j] = pq[j], pq[i]\n\tpq[i].index = i\n\tpq[j].index = j\n}\n\nfunc (pq *PriorityQueue) Push(x interface{}) {\n\tn := len(*pq)\n\titem := x.(*Item)\n\titem.index = n\n\t*pq = append(*pq, item)\n}\n\nfunc (pq *PriorityQueue) Pop() interface{} {\n\told := *pq\n\tn := len(old)\n\titem := old[n-1]\n\told[n-1] = nil  // avoid memory leak\n\titem.index = -1 // for safety\n\t*pq = old[0 : n-1]\n\treturn item\n}\n```
['Go']