Please consider the following question:
There are n
presentations at a conference and each has a start time and a finishing time. You cannot attend all of them because some of them overlap. Each presentations have values corresponding to your desire to attend them.
In O(n log n)
time, use a dynamic programming algorithm to find a set of presentations with maximal total value such that none of their times overlap.
My thoughts:
By using dynamic programming, we would check each presentation, storing its start time, finishing time, value, one at a time (and compare if overlaps previous stored data). However, how would do this in O(n log n)
time?
Sort the interval by end time (this is
O(nlogn)
), then apply the DP solution that follows the recursive formulas:In the above recursive formula:
j
is done by binary search easily (sinceend
is sorted).i
of the recursive function is the current "candidate" presentation.D(j)
) - or you don't and go on to the next candidate.D(n)
.D(j)+values[i]
in themax{}
function - addi
, otherwise - you don't attend it.