Skip to content

Commit 5c07a99

Browse files
authored
Create Q-03: Best Time to Buy and Sell Stock III.java
1 parent dab63c3 commit 5c07a99

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
123. Best Time to Buy and Sell Stock III
3+
Hard
4+
5+
You are given an array prices where prices[i] is the price of a given stock on the ith day.
6+
Find the maximum profit you can achieve. You may complete at most two transactions.
7+
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
8+
9+
Example 1:
10+
Input: prices = [3,3,5,0,0,3,1,4]
11+
Output: 6
12+
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
13+
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
14+
15+
Example 2:
16+
Input: prices = [1,2,3,4,5]
17+
Output: 4
18+
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
19+
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
20+
21+
Example 3:
22+
Input: prices = [7,6,4,3,1]
23+
Output: 0
24+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
25+
26+
Constraints:
27+
28+
1 <= prices.length <= 105
29+
0 <= prices[i] <= 105
30+
*/
31+
32+
class Solution {
33+
public int maxProfit(int[] prices) {
34+
//1. Recursion::
35+
//return solveRec(prices,0,0,0);
36+
37+
//2. Memoization::
38+
// 3 chnaging parameter:) Use either 3dp or hashmap:)
39+
// HashMap<String,Integer> map =new HashMap<>();
40+
// return solveMemo(prices,0,0,0,map);
41+
42+
//return solveTab(prices);
43+
44+
return solveTabSpaceOptmised(prices);
45+
46+
}
47+
public static int solveRec(int[] prices,int indx,int buy,int cnt){
48+
if(indx==prices.length) return 0;
49+
if(cnt>2) return 0;
50+
51+
int profit=0;
52+
if(buy==0){
53+
// buy::
54+
profit =-prices[indx]+solveRec(prices,indx+1,1,cnt+1);
55+
}else{
56+
//sell::
57+
profit =prices[indx]+solveRec(prices,indx+1,0,cnt);
58+
}
59+
int skip =solveRec(prices,indx+1,buy,cnt);
60+
return Math.max(skip,profit);
61+
}
62+
public static int solveMemo(int[] prices,int indx,int buy,int cnt,HashMap<String,Integer> map){
63+
if(indx==prices.length) return 0;
64+
if(cnt>2) return 0;
65+
66+
String key = indx+"-"+buy+"-"+cnt;
67+
if(map.containsKey(key)){
68+
return map.get(key);
69+
}
70+
int profit=0;
71+
if(buy==0){
72+
// buy::
73+
profit =-prices[indx]+solveMemo(prices,indx+1,1,cnt+1,map);
74+
}else{
75+
//sell::
76+
profit =prices[indx]+solveMemo(prices,indx+1,0,cnt,map);
77+
}
78+
int skip =solveMemo(prices,indx+1,buy,cnt,map);
79+
map.put(key,Math.max(skip,profit));
80+
return Math.max(skip,profit);
81+
}
82+
public static int solveTab(int[] prices){
83+
//Tabulation::)
84+
int n=prices.length;
85+
int tab[][][] = new int[n+1][2][3];
86+
87+
for(int ind=n-1;ind>=0;ind--){
88+
for(int buy=0;buy<=1;buy++){
89+
for(int cap=1;cap<=2;cap++){
90+
if (buy == 0) {
91+
tab[ind][buy][cap] = Math.max(0 + tab[ind + 1][0][cap],
92+
-prices[ind] + tab[ind + 1][1][cap]);
93+
}
94+
if (buy == 1) {
95+
tab[ind][buy][cap] = Math.max(0 + tab[ind + 1][1][cap],
96+
prices[ind] + tab[ind + 1][0][cap - 1]);
97+
}
98+
}
99+
}
100+
}
101+
return tab[0][0][2];
102+
}
103+
public static int solveTabSpaceOptmised(int[] prices){
104+
//Sapce Optimied:: Tabulation::)
105+
int n=prices.length;
106+
107+
int ahead[][] = new int[2][3];
108+
int curr[][] = new int[2][3];
109+
110+
for(int ind=n-1;ind>=0;ind--){
111+
for(int buy=0;buy<=1;buy++){
112+
for(int cap=1;cap<=2;cap++){
113+
if (buy == 0) {
114+
curr[buy][cap] = Math.max(0 + ahead[0][cap],
115+
-prices[ind] + ahead[1][cap]);
116+
}
117+
if (buy == 1) {
118+
curr[buy][cap] = Math.max(0 + ahead[1][cap],
119+
prices[ind] + ahead[0][cap - 1]);
120+
}
121+
}
122+
}
123+
for (int i = 0; i < 2; i++) {
124+
for (int j = 1; j < 3; j++) {
125+
ahead[i][j] = curr[i][j];
126+
}
127+
}
128+
}
129+
130+
return ahead[0][2];
131+
}
132+
}

0 commit comments

Comments
 (0)