Data Structures & AlgorithmsVery hardCodeforces Div2 D

A DP over prefixes of an array computes the number of ways to split it into segments where each segment's sum is at most L. Which complexity is achievable with a sliding-window minimum or two pointers, for an array of length n?

  1. A. O(n²), since each end must try every start
  2. B. O(2ⁿ), since every subset of cut points must be tested
  3. C. O(n) or O(n log n), since the valid starts of the last segment form a window that moves right as the end moves right
  4. D. O(n³), since a cubic interval DP is needed

Correct answer: C. O(n) or O(n log n), since the valid starts of the last segment form a window that moves right as the end moves right

Because all values are non-negative, the feasible starts for each end form a contiguous range that moves monotonically, so a deque or two pointers keeps the DP linear or near-linear.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic