-
Notifications
You must be signed in to change notification settings - Fork 6
/
BestTimeToBuyAndSellStockIV.java
39 lines (31 loc) · 1.25 KB
/
BestTimeToBuyAndSellStockIV.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.shivaprasad.february.day47;
import java.util.HashMap;
public class BestTimeToBuyAndSellStockIV {
public static void main(String[] args) {
System.out.println(maxProfit(2,new int[]{3,2,6,5,0,3}));
}
static int maxProfit(int k, int[] prices) {
return maxProfit(prices,0,1,k,new HashMap<String,Integer>());
}
static int maxProfit(int[] prices,int currentDay,int canBuy,int transCount,HashMap<String,Integer> memo)
{
if(currentDay >= prices.length || transCount <= 0)
return 0;
String currentKey = currentDay+"_"+canBuy+"_"+transCount;
if(memo.containsKey(currentKey))
return memo.get(currentKey);
if(canBuy == 1)
{
int idle = maxProfit(prices,currentDay+1,canBuy,transCount,memo);
int buy = -prices[currentDay] + maxProfit(prices,currentDay+1,0,transCount,memo);
memo.put(currentKey,Math.max(idle,buy));
}
else
{
int idle = maxProfit(prices,currentDay+1,canBuy,transCount,memo);
int sell = prices[currentDay] + maxProfit(prices,currentDay+1,1,transCount-1,memo);
memo.put(currentKey,Math.max(idle,sell));
}
return memo.get(currentKey);
}
}