← 返回 meta 的题目列表Maximum Profit with At Most K Stock Transactions
类型:online_judge
meta
Given an integer array representing the stock prices, where the i-th element represents the stock price on day i. You are allowed to complete at most k transactions, where each transaction consists of buying and selling a stock, and you cannot sell on the buy day. Design an algorithm to find the maximum profit you can achieve. Input: An integer array prices of length n, representing the prices. An integer k representing the maximum number of transactions allowed. Output: An integer representing the maximum profit that can be achieved. Constraints: 1 <= k <= 100, 0 <= prices[i] <= 1000. Example:
Input: k = 2, prices = [2, 4, 1]
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Input: k = 2, prices = [3, 2, 6, 5, 0, 3]
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Example
Input
2
2 4 1