Overview
You can complete at most two transactions. We model this as a state machine with 4 states: after 1st buy, after 1st sell, after 2nd buy, after 2nd sell.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Maximize your profit given you can complete at most two transactions.
You can complete at most two transactions. We model this as a state machine with 4 states: after 1st buy, after 1st sell, after 2nd buy, after 2nd sell.
Each state can either stay as is (don't transact today) or transition to the new state by transacting at today's price.
Since the state on day i only depends on the state on day i-1, we can compress the DP table into just 4 variables.
The states are updated sequentially. We can safely update them in place because the updated previous state will only increase our current state's potential.