-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestTimeToBuyAndSellStock.java
More file actions
345 lines (285 loc) · 12 KB
/
BestTimeToBuyAndSellStock.java
File metadata and controls
345 lines (285 loc) · 12 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package Algorithms.DynamicProgramming;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 27 Feb 2025
* @link 121. Best Time to Buy and Sell Stock <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">LeetCode link</a>
* @topics Two Pointers, Dynamic Programming, Array
* @companies amazon, facebook, google, apple, bloomberg, microsoft, agoda, oracle, tiktok, zoho, uber, visa, zoox, ibm, atlassian, infosys, tcs, adobe, goldman, tesla, morgan, accenture, jpmorgan, yahoo, nvidia, bolt, walmart, yandex, paypal, samsung
*/
public class BestTimeToBuyAndSellStock {
public static void main(String[] args) {
int[] prices = {7,1,5,3,6,4};
System.out.println("maxProfit using l,r pointers => " + maxProfitUsingTwoPointers(prices));
System.out.println("maxProfit using two pointers (minPrice, i) => " + maxProfitUsingTwoPointers2(prices));
System.out.println("maxProfit using two pointers (effectiveBuyPrice, maxProfit) => " + maxProfitUsingTwoPointersEffectiveBuyPrice(prices));
System.out.println("maxProfit using bottom up tabulation dp => " + maxProfitUsingBottomUpTabulationDp(prices));
System.out.println("maxProfit using brute force => " + maxProfitUsingBruteForce(prices));
}
public static int maxProfitMyApproach(int[] prices) {
int profit = 0;
int buy = prices[0];
for(int price: prices) {
if(buy < price) {
int currProfit = price - buy;
profit = Math.max(profit, currProfit);
} else {
buy = price;
}
}
return profit;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
*
* Given [7,1,5,3,6,4]
* Now draw the graph based i on x-axis and item on y-axis
*
* prices[i] item
* ▲
* |
* |
* |
* |
* |
* 7 | *
* | |
* 6 | | *
* | | |
* 5 | | * |
* | | | |
* 4 | | | | *
* | | | | |
* 3 | | | * | |
* | | | | | |
* 2 | | | | | |
* | | | | | |
* 1 | | * | | | |
* |_____|______|______|_______|_______|_______|
* 0 1 2 3 4 5 -----► index i
* l r
* l r
* l r
* l r
* l r
*
* here we use if-else but not in {@link #maxProfitUsingTwoPointers2} cause it's optional, as minPrice(7,1) is 1
* and maxProfit(prevMaxProfit, 1-1) is always prev prevMaxProfit -- so no need to calculate again
*/
public static int maxProfitUsingTwoPointers(int[] prices) {
int maxProfit = 0, l = 0, r = 1;
while(r<prices.length){
if(prices[l] > prices[r]) l = r;
else maxProfit = Math.max(maxProfit, prices[r] - prices[l]); // or if(tempProfit > maxProfit) maxProfit = tempProfit;
r++;
}
return maxProfit;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
*
* This approach is same as above {@link #maxProfitUsingTwoPointers(int[])}
* but instead of l & r use minPrice and for loop
*
* And if you check {@link #maxProfitUsingBottomUpTabulationDp(int[])}, both uses dp, but here it is "Bottom-Up NoMemory DP".
* That's why this is two pointers approach
*/
public static int maxProfitUsingTwoPointers2(int[] prices) {
int maxProfit = 0;
int minPrice = Integer.MAX_VALUE;
for (int i = 0; i < prices.length; i++) {
minPrice = Math.min(minPrice, prices[i]); // the minPrice up to i
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
}
return maxProfit;
}
/**
minNum=p[0]=7 or Integer.MAX_VALUE
currSum=0
[7,1,5,3,6,4]
i
minNum=Math.min(minNum,p[i])=(7,1)=1
curSum=max(currSum,p[i]-minNum)= (0,1-1)
[7,1,5,3,6,4]
i
minNum=Math.min(minNum,p[i])=(1,5)=1
curSum=max(currSum,p[i]-minNum)=(0,5-1)=4
[7,1,5,3,6,4]
i
minNum=Math.min(minNum,p[i])=(1,3)=1
curSum=max(currSum,p[i]-minNum)=(4,3-1)=4
[7,1,5,3,6,4]
i
minNum=Math.min(minNum,p[i])=(1,6)=1
curSum=max(currSum,p[i]-minNum)=(4,6-1)=5
[7,1,5,3,6,4]
i
minNum=Math.min(minNum,p[i])=(1,4)=1
curSum=max(currSum,p[i]-minNum)=(5,4-1)=5
*/
public int maxProfitUsingTwoPointerMyApproachOld(int[] prices) {
int minNum = Integer.MAX_VALUE; // pointer 1
int currSum = 0;
for(int price: prices) { // pointer 2
minNum = Math.min(minNum, price);
currSum = Math.max(currSum, price-minNum);
}
return currSum;
}
// using effective buy price approach
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
*
* This approach is same as {@link #maxProfitUsingTwoPointers2} and {@link #maxProfitUsingTwoPointerMyApproachOld(int[])}
* but instead of minPrice, it uses effectiveBuyPrice
* see {@link Algorithms.DynamicProgramming.BestTimeToBuyAndSellStockII#maxProfitUsingTwoPointersEffectiveBuyPrice} for easier understanding
*/
public static int maxProfitUsingTwoPointersEffectiveBuyPrice(int[] prices) {
int maxProfit = 0, effectiveBuyPrice = Integer.MAX_VALUE;
for (int i = 0; i < prices.length; i++) {
effectiveBuyPrice = Math.min(effectiveBuyPrice, prices[i]); // the effective buy price up to i
maxProfit = Math.max(maxProfit, prices[i] - effectiveBuyPrice);
}
return maxProfit;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*
* Same as {@link #maxProfitUsingTwoPointers(int[])} but use two arrays minBuys & maxSells instead of l & r
*/
public static int maxProfitUsingTwoPointers3(int[] prices) { // [7, 1, 5, 3, 6, 4]
int n = prices.length;
int[] minBuys = new int[n], maxSells = new int[n];
minBuys[0] = prices[0];
maxSells[n-1] = prices[n-1];
// left to right for minBuys till each index
for(int i=1; i<n; i++) minBuys[i] = Math.min(minBuys[i-1], prices[i]); // [7, 1, 1, 1, 1, 1]
// right to left for maxSells till each index
for(int i=n-2; i>=0; i--) maxSells[i] = Math.max(maxSells[i+1], prices[i]); // [7, 6, 6, 6, 6, 4]
// now calculate the max profit
int maxProfit = 0;
for(int i=0; i<n; i++) maxProfit = Math.max(maxProfit, maxSells[i]-minBuys[i]);
return maxProfit;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*
* 2D dp array, dp[i][0] is val of minPrice till now, and dp[i][1] is the maxProfit till now
*
* i=0 [ 7, 1, 5, 3, 6, 4]
* dp = [[7][0], [][], [][], [][], [][], [][]]
*
* i=1 [ 7, 1, 5, 3, 6, 4]
* dp = [[7][0], [1][0], [][], [][], [][], [][]]
* [max(7,1)][max(0, 1-7)]
*
* i=2 [ 7, 1, 5, 3, 6, 4]
* dp = [[7][0], [1][0], [1][4], [][], [][], [][]]
* [max(1,5)][max(0, 5-1)]
*
* i=3 [ 7, 1, 5, 3, 6, 4]
* dp = [[7][0], [1][0], [1][4], [1][4], [][], [][]]
* [max(1,3)][max(4, 3-1)]
*
* i=4 [ 7, 1, 5, 3, 6, 4]
* dp = [[7][0], [1][0], [1][4], [1][4], [-1][5], [][]]
* [max(1,6)][max(4, -1+6)]
*
* i=4 [ 7, 1, 5, 3, 6, 4]
* dp = [[7][0], [1][0], [1][4], [1][4], [1][5], [1][5]]
* [max(1,4)][max(5, 4-1)]
*
*/
public static int maxProfitUsingBottomUpTabulationDp(int[] prices) {
int n = prices.length;
if (n == 0) return 0;
int[][] dp = new int[n][2];
dp[0][0] = prices[0]; // minPrice till now --> initially assume that we bought at i=0 i.e prices[0]
dp[0][1] = 0; // maxProfit till now - if we sold
for (int i = 1; i < n; i++) {
int prevMinPrice = dp[i - 1][0];
int prevMaxProfit = dp[i - 1][1];
// CURR minPrice till now
dp[i][0] = Math.min(prevMinPrice, prices[i]);
// CURR maxProfit till now
dp[i][1] = Math.max(prevMaxProfit, prices[i]-prevMinPrice);
}
return dp[n - 1][1];
}
/**
* same as {@link #maxProfitUsingBottomUpTabulationDp(int[])}
*
* but dp[i][0] is maxHold till now (i.e minPrice) and dp[i][1] is maxNotHold till now (i.e maxProfit)
*/
public static int maxProfitUsingBottomUpTabulationDp2(int[] prices) {
int n = prices.length;
if (n == 0) return 0;
int[][] dp = new int[n][2];
dp[0][0] = -prices[0]; // minPrice till now - hold --> initially assume that we bought at i=0 i.e prices[0] so the amount we spent will be negative i.e -7
dp[0][1] = 0; // maxProfit till now - if we sold or not hold or never bought
for (int i = 1; i < n; i++) {
int prevMaxHold = dp[i - 1][0];
int prevMaxNotHold = dp[i - 1][1];
// CURR maxHold
dp[i][0] = Math.max(prevMaxHold, -prices[i]); // hold the min price -- here we use max as the hold is in -ve value
// CURR maxNotHold
dp[i][1] = Math.max(prevMaxNotHold, prevMaxHold + prices[i]); // Either keep "not holding" (carry over previous), or sell today (add today's price to previous holding).
}
return dp[n - 1][1];
}
/**
* @TimeComplexity O(n^2)
* @SpaceComplexity O(1)
*/
public static int maxProfitUsingBruteForce(int[] prices) {
int n = prices.length;
int maxProfit = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
maxProfit = Math.max(maxProfit, prices[j] - prices[i]);
}
}
return maxProfit;
}
/**
* WORKING BUT ALL TEST CASES ARE NOT PASSING
THOUGHTS:
---------
1) Need max diff at any point of day
2) So, calculate at each i --> brute force n^2 => 10^10 which is not good
3) or maintain the currMax
3) Or use dp[] to maintain the max num
*/
public int maxProfitMyApproachOld(int[] prices) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i=0; i<prices.length; i++) {
List<Integer> l = map.getOrDefault(prices[i], new ArrayList<>());
l.add(i);
map.put(prices[i], l);
}
List<Integer> lst = new ArrayList<>(map.keySet()); // or get sorted keys in reverse order
int diff = 0;
for (int p: prices) {
// System.out.println(map);
System.out.println(lst);
boolean isPMax = p == lst.get(lst.size()-1);
map.get(p).remove(0);
if (map.get(p).size() == 0 ) {
map.remove(p);
lst=new ArrayList<>(map.keySet());
}
if (isPMax) continue;
diff = Math.max(diff, lst.get(lst.size()-1)-p);// lst.get(n-1) is the max num
System.out.println("p: " + p + ", diff: "+ diff + ", max: " + lst.get(lst.size()-1) + "\n");
}
return diff;
}
}