You have a list of videos in a feed. Video i has duration d[i] (positive integer). A user has an “attention span” limit A.
You want to select a subset of videos to watch (keeping the original order, i.e., choose a subsequence of indices i1 < i2 < ... < ik) to maximize total watched time:
maximize ∑t=1kd[it]
subject to the constraint that any two consecutively watched videos do not exceed the attention span when added:
d[it]+d[it+1]≤Afor all t=1..k−1
Task
Write an algorithm/function that returns the maximum total watched duration achievable.
Assumptions / clarifications
-
n = len(d)
can be large enough that brute-forcing all subsets is too slow.
-
Watching 0 or 1 video is always allowed.
Follow-up
If the user is allowed to watch the same video multiple times, how would you modify the approach?
-
To avoid an unbounded/infinite answer, assume there is an additional limit
K
= maximum number of videos the user can watch total (including repeats). Return the maximum total watched duration under the same consecutive-sum constraint.