Process Tasks Using Servers
Medium · Heap & Priority Queue · about 30 minutes
Asked at Amazon, Google, Microsoft, ByteDance.
servers[i] is the weight of server i; tasks[j] is the time task j takes. Task j arrives at second j and waits in a queue. Whenever servers are free and tasks are waiting, the earliest-arrived task goes to the free server with the smallest weight (ties: smallest index). If no server is free, tasks wait until one frees up; several tasks may start at the same moment. Return ans, where ans[j] is the server that runs task j.