dsa1 min read
Best Time to Buy/Sell Stock I — Single Transaction
Maximize profit from a single buy-sell. Track running minimum price; at each day profit = price - min_so_far.
Read →
webcoderspeed.com
6 articles
Maximize profit from a single buy-sell. Track running minimum price; at each day profit = price - min_so_far.
Maximize profit with unlimited buy-sell transactions (but hold at most 1 share). Greedy: collect every upward slope.
Maximize profit with at most 2 transactions. Track 4 states: buy1, sell1, buy2, sell2. State machine DP.
At most k transactions. dp[t][i] = max profit using t transactions up to day i. If k >= n/2, unlimited transactions.
Unlimited transactions with 1-day cooldown after selling. 3-state DP: hold, sold (cooldown), rest.
Unlimited transactions with transaction fee per sell. 2-state DP: cash (not holding) and hold (holding stock).