← 返回 bloomberg 的题目列表Best Time to Buy and Sell Stock IV
类型:online_judge
Given an integer array price where price[i] is the price of a given stock on the i-th day, and an integer k representing the maximum number of transactions you are allowed, design an algorithm to find the maximum profit. You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Implement this function and return the maximum profit you can obtain.
Input Constraints:
0 <= k <= 10^9
0 <= price.length <= 10^5
0 <= price[i] <= 10^4
Test Cases:
Input: k = 2, prices = [2, 4, 1] Output: 2
Input: k = 2, prices = [3, 2, 6, 5, 0, 3] Output: 7
Example
Input
k = 2, prices = [2, 4, 1]