Overview
You can complete at most K transactions. We model this as a state machine where each transaction j up to K has its own buy and sell state.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Maximize your profit given you can complete at most k transactions.
You can complete at most K transactions. We model this as a state machine where each transaction j up to K has its own buy and sell state.
This is exactly like Stock III, but generalized to K transactions using arrays instead of discrete variables.
We only need the previous transaction's sell state to compute the current transaction's buy state, leading to O(K) space.
Notice how `sell[j-1]` is used to transition into `buy[j]`. We can use today's price to buy the stock using the profit we made from the previous j-1 transactions.