Problem
prices[i] is a stock's price on day i, and you may complete at most `k` trades. A trade is one buy followed later by one sell, and you can hold at most one share at a time. Return the largest total profit you can make.
Example 1
- Input
- prices = [5, 1, 4, 2, 9], k = 1
- Output
- 8
- Why
- buy 1, sell 9
Example 2
- Input
- prices = [2, 9, 1, 3, 1, 8], k = 2
- Output
- 14
- Why
- buy 2, sell 9; buy 1, sell 8
Example 3
- Input
- prices = [1, 5, 2, 8, 3, 9, 4, 10], k = 3
- Output
- 19
Constraints
1 <= k <= 1001 <= prices.length <= 10000 <= prices[i] <= 1000