Run
public class Main {
static int findMaximumProfit(int[] prices, int i, int k, int buy, int[][] v) {
// If no stock can be chosen
if (i >= prices.length || k <= 0) return 0;
if (v[i][buy] != -1) return v[i][buy];
int nbuy;
if (buy == 1)
nbuy = 0;
else
nbuy = 1;
if (buy == 1) {
return v[i][buy] = Math.max(-prices[i] + findMaximumProfit(prices, i + 1, k, nbuy, v),
findMaximumProfit(prices, i + 1, k, (int)(buy), v));
}
// Otherwise
else {
// Buy now
if (buy == 1)
nbuy = 0;
else
nbuy = 1;
return v[i][buy] =
Math.max(prices[i] + findMaximumProfit(prices, i + 1, k - 1, nbuy, v),
findMaximumProfit(prices, i + 1, k, buy, v));
}
}
static int maxProfit(int[] prices) {
int n = prices.length;
int[][] v = new int[n][2];
for (int i = 0; i < v.length; i++) {
v[i][0] = -1;
v[i][1] = -1;
}
return findMaximumProfit(prices, 0, 1, 1, v);
}
// Driver Code
public
static void main(String[] args) {
// Given prices
int[] prices = {7, 1, 5, 3, 6, 4};
int ans = maxProfit(prices);
// Print answer
System.out.println("Best time to buy and Sell stock = "+ans);
}
}
Login/Signup to comment