JavaScript Program to Find Maximum Profit by Buying and Selling a Share at Most Twice using Array
Last Updated :
27 Sep, 2023
A buyer buys shares in the morning and sells them on the same day. If the trader is allowed to make at most 2 transactions in a day, the second transaction can only start after the first one is complete (Buy->sell->Buy->sell). Given stock prices throughout the day, find out the maximum profit that a share trader could have made.
Input: price[] = {10, 22, 5, 75, 65, 80}
Output: 87
Trader earns 87 as sum of 12, 75
Buy at 10, sell at 22,
Buy at 5 and sell at 80
Input: price[] = {2, 30, 15, 10, 8, 25, 80}
Output: 100
Trader earns 100 as sum of 28 and 72
Buy at price 2, sell at 30, buy at 8 and sell at 80
There are different approaches to finding maximum profit by buying and selling a share at most twice:
Approach 1: Naive approach
In this approach, we are going to store the maximum possible profit of every subarray and solve the problem in the following two phases.
- Create a table profit[0..n-1] and initialize all values in it to 0.
- Traverse price[] from right to left and update profit[i] such that profit[i] stores maximum profit achievable from one transaction in subarray price[i..n-1]
- Traverse price[] from left to right and update profit[i] such that profit[i] stores maximum profit such that profit[i] contains maximum achievable profit from two transactions in subarray price[0..i].
- Return profit[n-1]
Example: This example shows the use of the above-explained approach.
Javascript
function maxProfit(price, n) {
let profit = new Array(n);
for (let i = 0; i < n; i++)
profit[i] = 0;
let max_price = price[n - 1];
for (let i = n - 2; i >= 0; i--) {
if (price[i] > max_price)
max_price = price[i];
profit[i] = Math.max(profit[i + 1],
max_price - price[i]);
}
let min_price = price[0];
for (let i = 1; i < n; i++) {
if (price[i] < min_price)
min_price = price[i];
profit[i] = Math.max(profit[i - 1],
profit[i] + (price[i] - min_price));
}
let result = profit[n - 1];
return result;
}
let price = [2, 30, 15, 10, 8, 25, 80];
let n = price.length;
console.log( "Maximum Profit = " +
maxProfit(price, n));
|
Output
Maximum Profit = 100
Time complexity: O(n)
Auxiliary space: O(n)
Approach 2: Recursive Approach
In this approach, we have two choices: Buy/Sell this stock OR ignore it and move to the next one. Along with day, we also need to maintain a capacity variable which will tell us how many transactions are remaining and it will be of which type (Buy or Sell). According to that we will make recursive calls and calculate the answer. We can do at most 4 transactions (Buy, Sell, Buy, Sell) in this order.
Example: This example shows the use of the above-explained approach.
Javascript
function f(idx, buy, prices, cap, n) {
if (cap == 0) {
return 0;
}
if (idx == n) {
return 0;
}
let profit = 0;
if (buy == 0) {
profit = Math.max(
-prices[idx] + f(idx + 1, 1, prices, cap, n),
f(idx + 1, 0, prices, cap, n)
);
}
else {
profit = Math.max(
prices[idx] + f(idx + 1, 0, prices, cap - 1, n),
f(idx + 1, 1, prices, cap, n)
);
}
return profit;
}
function maxtwobuysell(prices, n) {
return f(0, 0, prices, 2, n);
}
const arr = [2, 30, 15, 10, 8, 25, 80];
const size = arr.length;
console.log(maxtwobuysell(arr, size));
|
Time Complexity : O(2^N)
Auxiliary Space : O(N)
Share your thoughts in the comments
Please Login to comment...