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
You are given an array prices where prices[i] is the price of a given stock on the ith day.
7
+
8
+
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
9
+
10
+
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
11
+
12
+
13
+
14
+
Example 1:
15
+
16
+
Input: prices = [7,1,5,3,6,4]
17
+
Output: 5
18
+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
19
+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
20
+
Example 2:
21
+
22
+
Input: prices = [7,6,4,3,1]
23
+
Output: 0
24
+
Explanation: In this case, no transactions are done and the max profit = 0.
25
+
26
+
Constraints:
27
+
28
+
1 <= prices.length <= 10^5
29
+
0 <= prices[i] <= 10^4
30
+
*/
31
+
/*
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
35
+
36
+
Then we initialize our max_profit as 0.
37
+
38
+
Now we will start our while loop and we will run till our
39
+
Right pointer less then length of array
40
+
For Example:
41
+
prices=[7,1,5,3,6,4]
42
+
Note:
43
+
prices[left] --> buy stock
44
+
prices[right] --> sell stock
45
+
now we will check price at right and left pointer
46
+
47
+
step 1:
48
+
49
+
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
51
+
52
+
step 2:
53
+
54
+
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 max_profit and move our right pointer alone
56
+
57
+
step 3:
58
+
59
+
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 max_profit previously it
61
+
62
+
was 4 now our current profit is 2 so we will check which is maximum and update our max_profit and move our right pointer alone
63
+
64
+
step 4:
65
+
66
+
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 max_profit previously it was 4 now our current profit is 5 so we will check which is maximum and update our max_profit and move our right pointer alone
68
+
69
+
step 5:
70
+
71
+
price[left]=1 price[right]=4 profit=3
72
+
same logic as above
73
+
*/
74
+
75
+
constmaxProfit=(prices)=>{
76
+
letleft=0;// Buy
77
+
letright=1;// sell
78
+
letmax_profit=0;
79
+
while(right<prices.length){
80
+
if(prices[left]<prices[right]){
81
+
letprofit=prices[right]-prices[left];// our current profit
Copy file name to clipboardExpand all lines: README.md
+1
Original file line number
Diff line number
Diff line change
@@ -72,6 +72,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil
72
72
|[Gas Station](/LeetcodeProblems/Algorithms/medium/GasStation/index.js)| Medium |https://leetcode.com/problems/gas-station/description/|
73
73
| [K Closest Points to Origin](/LeetcodeProblems/Algorithms/medium/K_Closest_Points_to_Origin.js/) | Medium | https://leetcode.com/problems/k-closest-points-to-origin/
74
74
|[BestTimeToBuy](LeetcodeProblems/Algorithms/easy/Best_Time_To_Buy_And_Sell_Stock_II.js)| Medium |https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii|
75
+
| [Best Time to Buy and Sell Stock](/LeetcodeProblems/Algorithms/easy/Best_Time_to_buy_and_sell_stock.js) | Easy | https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
75
76
|[Flood Fill ](/LeetcodeProblems/Algorithms/easy/Flood_Fill.js)| Easy |https://leetcode.com/problems/flood-fill/|
76
77
|[Implement stack using queues ](/LeetcodeProblems/Algorithms/easy/Implement_stack_using_queues.js)| Easy |https://leetcode.com/problems/implement-stack-using-queues/|
77
78
|[Number of Segments in a String ](/LeetcodeProblems/Algorithms/easy/Number_of_Segments_in_a_String.js)| Easy |https://leetcode.com/problems/number-of-segments-in-a-string/|
0 commit comments