The task scheduling problem for n tasks is solved by greedy algorithm. I have encountered this particular sort of problem in various coding challenges which asks to find out minimum of maximum overshoot dynamically. One of them is stated below:
Interview Street Problem:
You have a long list of tasks that you need to do today. Task i is specified by the deadline by which you have to complete it (Di) and the number of minutes it will take you to complete the task (Mi). You need not complete a task at a stretch. You can complete a part of it, switch to another task and then switch back.
You've realized that it might not actually be possible complete all the tasks by their deadline, so you have decided to complete them so that the maximum amount by which a task's completion time overshoots its deadline is minimized.
My Approach
Now consider an intermediate stage where we have found the solution for i-1 tasks and have arranged them in sorted order. We have also stored the index of the task which had the maximum overshoot with i-1 tasks say maxLate. After the arrival of the *i*th task we check if D[i] < D[maxlate] then the new maxLate will be either old maxLate of the ith task. I am confused for the case when D[i] > D[maxlate]. Is a linear scan necessary for this case? Also suggest a good data structure for updating the new list and keeping them in sorted order. Thanks for your help.
First of all, you need to prove that given a set of task
(m_i, d_i)
, the best schedule is finish the jobs according to their deadlines, i.e. emergent jobs first.And the problem is equivalent to:
This algorithm run in O(N^2) where N is the number of jobs, which is too slow for getting accepted in interview street. However, we can use some advanced data structure, to speed up the
insert
andquery
operation.I use a segment tree with lazy update to solve this problem in O(NlgN) time, and here's my code