Quillstone's CI system runs builds on m identical machines. Each job has an id, a submit time, a duration and a priority, where a lower number is more urgent.
Whenever a machine is free and jobs are waiting, it starts the waiting job with the lowest priority number; ties go to the earlier submit time, then the smaller id. A running job is never interrupted. A machine that frees up at time t can start a job submitted at time t.
Write finishTimes(m, jobs), where each job is [id, submit, duration, priority], and return each job's finish time in the order the jobs were given.
Example
m = 2
jobs = [[1,0,5,2], [2,0,3,1], [3,1,4,1], [4,2,2,3], [5,3,1,0]]
Output: [5, 3, 8, 7, 4]
At time 3 job 2 finishes and job 5 arrives with priority 0, so it starts ahead of job 3, which has waited since time 1.
Constraints
- Up to 200,000 jobs, not sorted by submit time;
1 <= m <= 10,000.
- Times and durations are integers up to
10^9; durations are at least 1.
What to discuss
Which data structures you need and why, how you skip idle stretches when no job is waiting, and the complexity. If time allows: low-priority jobs can starve. How would you let a job's priority improve the longer it waits, and what does that do to your data structures?