Data Structures & AlgorithmsVery hardCodeforces Div2 D
You split the array [1, 15, 7, 9, 2, 5, 10] into contiguous non-empty segments, each of length at most 3. A segment contributes (its maximum) × (its length). What is the largest total you can get?
- A. 82
- B. 75
- C. 84
- D. 90
Correct answer: C. 84
dp[i] is the best total for the prefix of length i; the last segment has length 1 to 3 and value (its maximum) × (its length). Cutting fixed blocks of 3 from the left is not optimal, because where the cut falls matters.
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
- What is the maximum product of a non-empty contiguous subarray of [-2, 3, -4, 0, -1, 5, -3, -2]?
- How many binary strings of length 12 contain no three consecutive 1s and end with the digit 0?
- Coins of denominations 1, 3 and 4 are available in unlimited supply. What is the minimum number of coins needed to make exactly 6?
- What is the minimum number of perfect squares (1, 4, 9, 16, ...) whose sum is exactly 12? Repeats are allowed.
- Stock prices by day are [3, 3, 5, 0, 0, 3, 1, 4, 9, 2]. You may complete at most TWO buy-then-sell transactions, and you must sell before…
- Daily prices are [1, 2, 3, 0, 2, 5, 1, 6]. You may buy or sell one share per day, hold at most one share, and after SELLING you must wait…