My main problem: how can I find tasks that were previously performed? Am I using a support data structure?
The trick is that you do not need to know which tasks have already been completed. Because you can execute them in ascending order of time.
Say some kind of optimal solution (giving maximum profit) requires you to complete task A (deadline 10 ), and then task B (deadline 3 ). But in this case, you can safely replace A and B Both of them will still be completed on time, and the new arrangement will yield the same total profit.
The end of the proof.
How would you write a recurrence equation?
You already have a general idea, but you don't need a loop (min for 1 <= i <= n).
max_profit(current_job, start_time) // skip this job result1 = max_profit(current_job + 1, start_time) // start doing this job now finish_time = start_time + T[current_job] if finish_time <= D[current_job] // only if we can finish it before deadline result2 = max_profit(current_job + 1, finish_time) + V[current_job]; end return max(result1, result2); end
Converting it to DP should be trivial.
If you do not need O(n*max_deadline) (for example, when the values ββof d and t are large), you can resort to recursion with memoization and save the results in a hash table instead of a two-dimensional array.
change
If all tasks must be completed, but not all will be paid, the problem remains the same. Just click on assignments for which you do not have time (assignments that you cannot complete before the deadline) to the end. It's all.
source share