Best Time To Buy And Sell Stock Iii Problem
Description
LeetCode Problem 123.
You are given an array prices where prices[i] is the price of a given stock on the i^th day. Find the maximum profit you can achieve. You may complete at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:
Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
Example 4:
Input: prices = [1]
Output: 0
Constraints:
- 1 <= prices.length <= 10^5
- 0 <= prices[i] <= 10^5
Solution
Approach: Dynamic Programming with Four States
With at most two transactions, at any moment we are in one of four situations: we have made the first buy, the first sell, the second buy, or the second sell. For each one we keep the best balance we could have so far, where buying subtracts the price and selling adds it:
buy1: best balance after the first buy,max(buy1, -price).sell1: best balance after the first sell,max(sell1, buy1 + price).buy2: best balance after the second buy,max(buy2, sell1 - price). The second buy is paid with the profit of the first sell.sell2: best balance after the second sell,max(sell2, buy2 + price).
Each state either stays as it was (we do nothing today) or is reached from the previous state by acting at today's price. Start with buy1 = buy2 = -prices[0] and sell1 = sell2 = 0, then update the four values for each price in that order. The answer is sell2.
It looks wrong to update all four with the same day's price, since that seems to allow buying and selling on the same day. But a same-day buy and sell earns exactly 0, so it never creates profit that does not exist. It also covers the cases with fewer trades: one transaction is a second transaction of 0 profit, and no transaction leaves sell2 = 0.
For [3, 3, 5, 0, 0, 3, 1, 4], the best is buy at 0, sell at 3, buy at 1, sell at 4, so sell2 ends at 6.
Complexity: O(n) time and O(1) space, where n is the length of prices.
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# Best balance so far after each step (buying costs the price, selling earns it).
buy1 = -prices[0] # after the first buy
sell1 = 0 # after the first sell
buy2 = -prices[0] # after the second buy
sell2 = 0 # after the second sell
for price in prices:
buy1 = max(buy1, -price)
sell1 = max(sell1, buy1 + price)
buy2 = max(buy2, sell1 - price)
sell2 = max(sell2, buy2 + price)
return sell2
Now write it from memory
Close the write-up and solve it yourself against the tests.