-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathBuyAndSellStockAtmostTwice.js
59 lines (54 loc) · 2.09 KB
/
BuyAndSellStockAtmostTwice.js
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
/**
* @author Anirudh Sharma
*
* You are given an array prices where prices[i] is the price of a given stock on the ith day.
*
* Find the maximum profit you can achieve. You may complete at most two transactions.
*
* Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the
* stock before you buy again).
*/
const maxProfit = (prices) => {
// Special cases
if (prices === undefined || prices.length < 2) {
return 0;
}
// First buying price
let firstBuyingPrice = Number.NEGATIVE_INFINITY;
// First selling price
let firstSellingPrice = 0;
// Second buying price
let secondBuyingPrice = Number.NEGATIVE_INFINITY;
// Second selling price
let secondSellingPrice = 0;
// Loop for every element in the array
for (let price of prices) {
// If we are buying then we subtract the current
// price value from 0 for the first transaction
// because we have 0 profit before the first transaction
firstBuyingPrice = Math.max(firstBuyingPrice, -price);
// Now we need to sell the stock and maximize the profit
// And since we are selling, we will add the current price
// to our profit
firstSellingPrice = Math.max(firstSellingPrice, firstBuyingPrice + price);
// Now we have to buy the second stock and we have capital
// equals to the firstSellingPrice and since we are buying,
// we need to subtract the current price
secondBuyingPrice = Math.max(secondBuyingPrice, firstSellingPrice - price);
// Second selling price will be the final output and since
// we are selling, we will add the current price
secondSellingPrice = Math.max(secondSellingPrice, secondBuyingPrice + price);
}
return secondSellingPrice;
};
const main = () => {
let prices = [3, 3, 5, 0, 0, 3, 1, 4];
console.log(maxProfit(prices));
prices = [1, 2, 3, 4, 5];
console.log(maxProfit(prices));
prices = [7, 6, 4, 3, 1];
console.log(maxProfit(prices));
prices = [1];
console.log(maxProfit(prices));
};
main();