1
var assignTasks = function (servers, tasks) {2
const idleQueue = new PriorityQueue({3
compare: (a, b) => a.weight - b.weight || a.index - b.index,5
const processQueue = new MinPriorityQueue({6
priority: ({ completeSeconds }) => completeSeconds,8
let currentSeconds = 0;10
servers.forEach((weight, index) => idleQueue.enqueue({ weight, index }));12
return tasks.map((seconds, index) => {13
if (idleQueue.isEmpty())14
currentSeconds = processQueue.front().element.completeSeconds;15
if (currentSeconds < index) currentSeconds = index;17
!processQueue.isEmpty() &&18
processQueue.front().element.completeSeconds <= currentSeconds20
const processed = processQueue.dequeue().element;22
idleQueue.enqueue(processed);24
const idle = idleQueue.dequeue();25
const completeSeconds = currentSeconds + seconds;27
processQueue.enqueue({ ...idle, completeSeconds });