-
Notifications
You must be signed in to change notification settings - Fork 985
/
Copy pathBestTimeToBuyAndSellStockOne.java
43 lines (28 loc) · 1.13 KB
/
BestTimeToBuyAndSellStockOne.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
40
41
42
43
//Question Link= https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
import java.util.*;
public class BestTimeToBuyAndSellStockOne{
public static void main(String[] args) throws Exception {
int prices[]={1,3,4,2,6,1,6};
System.out.println(maxProfit(prices));
}
public static int maxProfit(int[] prices) {
Map<String,Integer> mp=new HashMap<>();
return solve(prices,0,true,1,mp);
}
public static int solve(int price[],int i,boolean canBuy,int count,Map<String,Integer> mp){
if(i>=price.length || count<=0) return 0;
String key=i+"!"+canBuy+"!"+count;
if(mp.containsKey(key)) return mp.get(key);
if(canBuy){
int idle=solve(price,i+1,canBuy,count,mp);
int buying=(-1*price[i])+solve(price,i+1,false,count,mp);
mp.put(key,Math.max(idle,buying));
return mp.get(key);
}else{
int idle=solve(price,i+1,canBuy,count,mp);
int selling=price[i]+solve(price,i+1,true,count-1,mp);
mp.put(key,Math.max(idle,selling));
return mp.get(key);
}
}
}