Skip to content

Commit c13e3f6

Browse files
Update Best_Time_to_buy_and_sell_stock.js
1 parent 691ed78 commit c13e3f6

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,44 @@ Constraints:
3030
*/
3131
/*
3232
Approach:
33-
let use initialize Left and Right pointer to first and second position of array
34-
Here Left is to buy stock and Right is to sell stock
33+
We will use a Two pointers strategy (Left and Right pointers).
34+
The first will start pointing to the first element, and the right to the second position of array.
35+
The Left is to buy stock and Right is to sell stock
3536
36-
Then we initialize our maxProfitValue as 0.
37+
We initialize our maxProfitValue as 0.
38+
39+
Now we will start our while loop, and we will run till our
40+
Right pointer less than the array's length.
3741
38-
Now we will start our while loop and we will run till our
39-
Right pointer less then length of array
4042
For Example:
4143
prices=[7,1,5,3,6,4]
4244
Note:
4345
prices[left] --> buy stock
4446
prices[right] --> sell stock
45-
now we will check price at right and left pointer
47+
We will check the price at the right and left pointer
4648
4749
step 1:
4850
4951
price[left]=7 price[right]=1 profit=-6
50-
here price[left] is greater than price[right] so we will move left pointer to the right position and increment our right pointer by 1. We always want our left point to be minimum
52+
here, price[left] is greater than price[right], so we will move the left pointer to the right position
53+
and increment our right pointer by 1. We always want our left point to be the minimum.
5154
5255
step 2:
5356
5457
price[left]=1 price[right]=5 profit=4
55-
here price[left] is less than price[right] which means we will get profit so we will update our maxProfitValue and move our right pointer alone
58+
here, price[left] is less than price[right], which means we will get profit,
59+
so we will update our maxProfitValue and move our right pointer alone
5660
5761
step 3:
5862
5963
price[left]=1 price[right]=3 profit=2
60-
here price[left] is less than price[right] which means we will get profit so we will check our maxProfitValue previously it
61-
62-
was 4 now our current profit is 2 so we will check which is maximum and update our maxProfitValue and move our right pointer alone
64+
here, price[left] is less than price[right], we will get profit, so we will compare the maxProfitValue with the current profit.
65+
We will update our maxProfitValue and move our right pointer alone
6366
6467
step 4:
6568
6669
price[left]=1 price[right]=6 profit=5
67-
here price[left] is less than price[right] which means we will get profit so we will check our maxProfitValue previously it was 4 now our current profit is 5 so we will check which is maximum and update our maxProfitValue and move our right pointer alone
70+
same logic as above
6871
6972
step 5:
7073
@@ -89,4 +92,4 @@ const maxProfit = (prices) => {
8992
return maxProfitValue;
9093
};
9194

92-
module.exports.maxProfit = maxProfit;
95+
module.exports.maxProfit = maxProfit;

0 commit comments

Comments
 (0)