rates[i] is its price on day i. A trade is buying the coin and selling it later. You can hold at most one coin at a time, so a new purchase is only possible after the previous coin is sold (the same day is fine).
You may make at most two trades. Return the largest total profit; if no profit is possible, return 0.rates=[4, 9, 2, 2, 7, 1, 6] → 10
Buy at 4, sell at 9 (+5); buy at 2, sell at 7 (+5). That is 10 in total; a third trade (1 → 6) is not allowed.
rates=[1, 3, 5, 8] → 7
One trade 1 → 8 earns 7, more than any two trades.
rates=[9, 6, 6, 2] → 0
The price only falls, so the best move is to do nothing: 0.
rates=[4, 9, 2, 2, 7, 1, 6]
10
Run checks the first 3 tests in your browser. Submit checks all 120 tests on the server, hidden ones included.