Best Time to Buy and Sell Stock III
HardDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.prices[i] is a stock's price on day i. You may complete at most two trades, where a trade is one buy followed later by one sell, and you can hold at most one share at a time (sell before you buy again). Return the largest total profit you can make, or 0 if no trade helps.
prices = [6, 2, 5, 3, 9, 4, 8] → 11 (buy 2, sell 9; buy 4, sell 8)
prices = [3, 4, 6, 7, 11] → 8 (one trade is enough)
prices = [9, 7, 5, 3] → 0Constraints
1 <= prices.length <= 10^50 <= prices[i] <= 10^5