To understand the hidden logic, you have to understand what these 4 variables stand for.
sell2: Final profit. buy2: Best profit so far, if you buy the stock not after Day i (2nd transaction). sell1: Best profit so far, if you sell the stock not after Day i (1st transaction). buy1: Minimum price to buy the stock.
The logic between buy1 and sell1 is quite straight forward. What you need to do is simply find a minimum price to buy and sell it some days after. Of course, sell1 won't update if the profit is not greater than before even if you buy the stock at a lower price. Let's assume you sell the stock at Day a to get the greatest profit for the 1st transaction, which stores in sell1.
Now comes the trick. Assume you find a better deal at Day b, sell1 get updated. So you have 2 choice for buy2:
not update buy2, you still sell your stock at Day a. Nothing changed. update buy2 with new sell1, which means you sell the stock at Day b. buy2 = sell1 - price[i] means you sell you stock at Day b and buy it at Day i. And Day i is definitely not early than Day b, which is the hidden logic.
近期评论