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?
- A. O(n²), since each end must try every start
- B. O(2ⁿ), since every subset of cut points must be tested
- 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
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Which problem is the standard application of 'binary search on the answer' where the feasibility test itself is a greedy scan?
- In bitmask DP over subsets of n items, what is the typical total time for the TSP-style recurrence dp[mask][last] with transitions to…
- Which statement about the 'house robber' DP on a line is correct?
- A greedy solution to a scheduling problem sorts jobs by profit and places each into the latest free slot before its deadline. What must…
- A coin-change problem uses coins {1, 3, 4} and asks for the minimum number of coins for each amount. Which statement is correct?
- In the circular array [1, 2, 1], what is the next greater element of the element at index 2 (search continues around the circle, -1 if no…