Gamified challenge · State-machine DP
prices[i] is the price of one share on day i. You may buy and sell as many times as you like, but you can hold at most one share at a time, and after you sell you must skip the next day (no buying on it). You may buy and sell only at that day's price, and you don't have to trade at all.
Write maxProfit(prices) (Python max_profit) that returns the largest total profit you can make. With no profitable trade the answer is 0.
Example: for [1, 2, 3, 0, 2] buy at 1, sell at 2, rest on day 2, buy at 0, sell at 2: the answer is 3.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google