|
| 1 | +/* |
| 2 | +121. Best Time to Buy and Sell Stock |
| 3 | +https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ |
| 4 | +
|
| 5 | +Problem: |
| 6 | +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 | +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 |
| 36 | +
|
| 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. |
| 41 | +
|
| 42 | +For Example: |
| 43 | +prices=[7,1,5,3,6,4] |
| 44 | +Note: |
| 45 | +prices[left] --> buy stock |
| 46 | +prices[right] --> sell stock |
| 47 | +We will check the price at the right and left pointer |
| 48 | +
|
| 49 | +step 1: |
| 50 | +
|
| 51 | +price[left]=7 price[right]=1 profit=-6 |
| 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. |
| 54 | +
|
| 55 | +step 2: |
| 56 | +
|
| 57 | +price[left]=1 price[right]=5 profit=4 |
| 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 |
| 60 | +
|
| 61 | +step 3: |
| 62 | +
|
| 63 | +price[left]=1 price[right]=3 profit=2 |
| 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 |
| 66 | +
|
| 67 | +step 4: |
| 68 | +
|
| 69 | +price[left]=1 price[right]=6 profit=5 |
| 70 | +same logic as above |
| 71 | +
|
| 72 | +step 5: |
| 73 | +
|
| 74 | +price[left]=1 price[right]=4 profit=3 |
| 75 | +same logic as above |
| 76 | +*/ |
| 77 | + |
| 78 | +const maxProfit = (prices) => { |
| 79 | + let left = 0; // Buy |
| 80 | + let right = 1; // sell |
| 81 | + let maxProfitValue = 0; |
| 82 | + while (right < prices.length) { |
| 83 | + if (prices[left] < prices[right]) { |
| 84 | + let profit = prices[right] - prices[left]; // our current profit |
| 85 | + |
| 86 | + maxProfitValue = Math.max(maxProfitValue, profit); |
| 87 | + } else { |
| 88 | + left = right; |
| 89 | + } |
| 90 | + right++; |
| 91 | + } |
| 92 | + return maxProfitValue; |
| 93 | +}; |
| 94 | + |
| 95 | +module.exports.maxProfit = maxProfit; |
0 commit comments