Algorithm
Problem Name: 188. Best Time to Buy and Sell Stock IV
You are given an integer array prices
where prices[i]
is the price of a given stock on the ith
day, and an integer k
.
Find the maximum profit you can achieve. You may complete at most k
transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
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.
Example 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.
Constraints:
1 <= k <= 100
1 <= prices.length <= 1000
0 <= prices[i] <= 1000
Code Examples
#1 Code Example with C Programming
Code -
C Programming
int _max(int a, int b) {
return a > b ? a : b;
}
int all_profits(int* prices, int pricesSize) {
int i, d, p;
p = 0;
for (i = 1; i < pricesSize; i ++) {
d = prices[i] - prices[i - 1];
p = d > 0 ? p + d : p; // get it as long as it is a profit!
}
return p;
}
int maxProfit(int k, int* prices, int pricesSize) {
int *b, *s, *buff, i, j, p;
if (pricesSize < 2) return 0;
if (k >= pricesSize / 2) return all_profits(prices, pricesSize);
buff = malloc((2 * k + 1) * sizeof(int));
//assert(buff);
b = &buff[0];
s = &buff[k];
for (i = 0; i < k; i ++) {
b[i] = 0x80000000; // min integer
s[i] = 0;
}
s[k] = 0;
for (i = 0; i < pricesSize; i ++) {
for (j = 0; j < k; j ++) {
// profit on buy is current buy or last sale minus today's price
b[j] = _max(b[j], s[j] - prices[i]);
// profit on sale is current sale or last buy plus today's price
s[j + 1] = _max(s[j + 1], b[j] + prices[i]);
}
}
p = s[k];
free(buff);
return p;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
class Solution {
public int maxProfit(int k, int[] prices) {
if (k == 0) {
return 0;
}
int[][] dp = new int[k + 1][2];
for (int i = 0; i < = k; i++) {
dp[i][0] = 1000;
}
for (int i = 0; i < prices.length; i++) {
for (int j = 1; j < = k; j++) {
dp[j][0] = Math.min(dp[j][0], prices[i] - dp[j - 1][1]);
dp[j][1] = Math.max(dp[j][1], prices[i] - dp[j][0]);
}
}
return dp[k][1];
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const maxProfit = function(k, prices) {
if (!prices.length) return 0
let len = prices.length,
res = 0
if (k >= ~~(len / 2)) {
for (let i = 1; i < len; i++) {
res += Math.max(prices[i] - prices[i - 1], 0)
}
return res
}
const buy = new Array(k + 1).fill(Number.MIN_SAFE_INTEGER)
const sell = new Array(k + 1).fill(0)
for (let p of prices) {
for (let i = 1; i < = k; i++) {
buy[i] = Math.max(sell[i - 1] - p, buy[i])
sell[i] = Math.max(buy[i] + p, sell[i])
}
}
return sell[k]
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
class Solution:
def maxProfit(self, k, prices):
if k >= len(prices) // 2: return sum(sell - buy for sell, buy in zip(prices[1:], prices[:-1]) if sell - buy > 0)
dp = [[0, -float("inf")] for _ in range(k + 1)]
for p in prices:
for i in range(k + 1):
if i and dp[i - 1][1] + p > dp[i][0]: dp[i][0] = dp[i - 1][1] + p
if dp[i][0] - p > dp[i][1]: dp[i][1] = dp[i][0] - p
return dp[-1][0]
Copy The Code &
Try With Live Editor
Input
Output