rates[i] is its price on day i. You may make as many buy-then-sell trades as you like, but you can hold at most one coin at a time.
There is one rule: you cannot buy on the day right after a sale; that day is a rest day.
Return the largest total profit you can make.rates=[3, 5, 0, 0, 4] → 6
Buy at 3 and sell at 5 (+2), rest the next day, then buy at 0 and sell at 4 (+4). That is 6 in total.
rates=[1, 4, 2, 7] → 6
Selling at 4 would spend the day priced 2 on rest, leaving no room for a second trade. Holding to the end is better: 7 − 1 = 6.
rates=[4] → 0
There is only one day, so no profit is possible.
len(rates) ≤ 5000rates[i] ≤ 1000rates=[3, 5, 0, 0, 4]
6
Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.