Trains the technique from
LeetCode 309Best Time to Buy and Sell Stock with CooldownThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A reseller watches one component whose quoted price on day i is prices[i].
The rules of the desk are:
i, the earliest possible next purchase is day i + 2.Return the largest profit the reseller can finish with.
Example 1
Buying on day 2 at 1 and selling on day 3 at 10 gains 9. Buying on day 0 at 5 and selling on day 1 at 10 gains 5, and day 2 is then a rest day, so day 3 is the only day left to buy on and there is no later day to sell on.
Example 2
Every later quote is below every earlier one, so any completed trade loses money and trading nothing is allowed.
Example 3
Buying on day 1 at 1 and selling on day 2 at 3 gains 2, day 3 is the rest day, and buying on day 4 at 4 then selling on day 5 at 7 gains 3, for 5 in total.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def max_profit(prices: list[int]) -> int:public int maxProfit(int[] prices)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.