Your question is Buy Low Sell High DP. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Two Sigma's market analytics pipeline receives a chronological list of prices for a security. Given at most k transactions, compute the maximum possible profit by choosing when to buy and sell.
A transaction consists of one buy followed by one sell. You must sell before buying again, cannot hold more than one position at a time, and cannot buy or sell before the corresponding price is available. If no profitable strategy exists, return 0.
Implement max_profit(prices, k), where prices is a list of integers and k is a nonnegative integer. Return an integer representing the maximum profit. Each transaction's profit is sell_price - buy_price.
def max_profit(prices, k):