rates[i] is its price on day i. Each day you may buy the coin, sell it, or do nothing, but you can hold at most one coin at a time. Selling and buying on the same day is allowed.
There is no limit on the number of trades. Return the largest total profit you can make.rates=[5, 2, 6, 4, 9, 9] → 9
Buy at 2 and sell at 6 (+4), then buy at 4 and sell at 9 (+5). That is 9 in total.
rates=[1, 3, 6, 10] → 9
The price only rises: buy on the first day and sell on the last, 10 − 1 = 9. Selling and rebuying along the way adds nothing.
rates=[8, 8, 3] → 0
The price never goes up, so trading cannot help; the answer is 0.
len(rates) ≤ 3·10⁴rates[i] ≤ 10⁴rates=[5, 2, 6, 4, 9, 9]
9
Run checks the first 3 tests in your browser. Submit checks all 128 tests on the server, hidden ones included.