I have a question that is very similar to algorithm to find longest non-overlapping sequences.
The only difference to the linked question is that instead of finding the set of non-overlapping tuples that represent the longest sequence, I need to find the set of non-overlapping tuples that represent the maximum coverage, by which I mean the sum of the tuple lengths is maximum (a tuple length being last - first + 1
given the definition of tuple in the next sentence).
I represent my tuples differently than the linked problem. Instead of (starting index, length)
, I represent my tuples as (first, last)
; for example, the tuple (3,7)
represents the set of numbers [3, 4, 5, 6, 7]
. (A tuple overlaps another tuple even if the end-points match; i.e., (2,6)
and (6,8)
overlap and therefore cannot both appear in the solution.)
As an example, consider the following set of tuples, sorted by first
:
[(0,100), (2,50), (30,150), (60,95), (110,190), (120,150), (191,200)]
The maximal set here would be [(0,100), (110,190), (191,200)]
with a coverage of 101 + 81 + 10 = 192
. (Note that the tuples in this solution are non-overlapping.)
What is an example of the least complex algorithm to solve this, and what is the complexity of that algorithm? (It would be just great if this could be solved in O(N)
, but I don't know at the moment if it can be.)
ADDENDUM: In retrospect, it turns out that the question I am asking here is equivalent to the weighted interval scheduling problem. This is a special case of the interval scheduling problem.
@j_random_hacker's answer, below, is, in fact, the known solution to the weighted interval scheduling problem, with a complexity in time of O(N log(N))
.
See Question&Answers more detail:
os