You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js
+16-13
Original file line number
Diff line number
Diff line change
@@ -30,41 +30,44 @@ Constraints:
30
30
*/
31
31
/*
32
32
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
35
36
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.
37
41
38
-
Now we will start our while loop and we will run till our
39
-
Right pointer less then length of array
40
42
For Example:
41
43
prices=[7,1,5,3,6,4]
42
44
Note:
43
45
prices[left] --> buy stock
44
46
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
46
48
47
49
step 1:
48
50
49
51
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.
51
54
52
55
step 2:
53
56
54
57
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
56
60
57
61
step 3:
58
62
59
63
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
63
66
64
67
step 4:
65
68
66
69
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
0 commit comments