Data Structures & AlgorithmsVery hardLeetCode-style · Codeforces Div2 C
Daily prices are [3, 2, 6, 5, 0, 3, 8, 1, 9]. You may complete at most 3 buy-then-sell transactions (one share at a time, sell before the next buy). What is the maximum profit?
- A. 16
- B. 24
- C. 17
- D. 20
Correct answer: D. 20
DP with two states per transaction count: buy[t] = max(buy[t], sell[t−1] − price) and sell[t] = max(sell[t], buy[t] + price). The number of transactions is a hard limit; a version without it (unlimited trades) gives a larger profit.
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 sum of a non-empty contiguous subarray of [4, -6, 3, -1, 7, -2, 5] that contains AT MOST one negative element?
- What is the maximum product of exactly 3 consecutive elements of [2, -3, 4, -1, 5, -6, 2]?
- How many contiguous subarrays (of length at least 2) of [1, 3, 4, 2, 5, 6, 8, 1] are strictly increasing?
- How many subarrays (i ≤ j) of [2, 1, 2, 3, 1, 2] have their first and last elements equal? A single element counts (its first and last are…
- Closed integer intervals are [(1, 4), (2, 6), (5, 9), (3, 3), (6, 8), (7, 12)]. What is the largest number of intervals that share a…
- How many indices i of [2, 2, 0, 2, 2] have the sum of the elements strictly to the left of i equal to the sum of the elements strictly to…