|
| 1 | +--- |
| 2 | +id: best-time-to-buy-sell-stock |
| 3 | +title: Best Time to Buy and Sell Stock Solution |
| 4 | +sidebar_label: 0121-Best-Time-to-Buy-and-Sell-Stock |
| 5 | +tags: |
| 6 | + - Best Time to Buy and Sell Stock |
| 7 | + - Array |
| 8 | + - LeetCode |
| 9 | + - Python |
| 10 | + - JavaScript |
| 11 | + |
| 12 | +description: "This is a solution to the Best Time to Buy and Sell Stock problem on LeetCode." |
| 13 | +--- |
| 14 | + |
| 15 | +In this page, we will solve the Best Time to Buy and Sell Stock problem using a two-pointer approach. We will provide the implementation of the solution in Python and JavaScript. |
| 16 | + |
| 17 | +## Problem Description |
| 18 | + |
| 19 | +You are given an array `prices` where `prices[i]` is the price of a given stock on the `i`-th day. 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. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return `0`. |
| 20 | + |
| 21 | +### Examples |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | + |
| 25 | +```plaintext |
| 26 | +Input: prices = [7,1,5,3,6,4] |
| 27 | +Output: 5 |
| 28 | +Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +```plaintext |
| 34 | +Input: prices = [7,6,4,3,1] |
| 35 | +Output: 0 |
| 36 | +Explanation: In this case, no transactions are done and the max profit = 0. |
| 37 | +``` |
| 38 | + |
| 39 | +### Constraints |
| 40 | + |
| 41 | +- `1 <= prices.length <= 10^5` |
| 42 | +- `0 <= prices[i] <= 10^4` |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +## Solution for Best Time to Buy and Sell Stock Problem |
| 47 | + |
| 48 | +### Intuition and Approach |
| 49 | + |
| 50 | +The problem can be solved using a two-pointer approach where we track the minimum price to buy and the maximum profit we can achieve at each step. |
| 51 | + |
| 52 | +<Tabs> |
| 53 | + <tabItem value="Two-pointer Approach" label="Two-pointer Approach"> |
| 54 | + |
| 55 | +### Approach: Two-pointer |
| 56 | + |
| 57 | +We use two pointers, `left` (to buy) and `right` (to sell), and iterate through the prices array to find the maximum profit. |
| 58 | + |
| 59 | +#### Implementation |
| 60 | + |
| 61 | +```jsx live |
| 62 | +function BestTimeToBuyAndSellStock() { |
| 63 | + const prices = [7, 1, 5, 3, 6, 4]; // Sample input |
| 64 | + |
| 65 | + const maxProfit = function (prices) { |
| 66 | + let left = 0; // Buy |
| 67 | + let right = 1; // Sell |
| 68 | + let max_profit = 0; |
| 69 | + |
| 70 | + while (right < prices.length) { |
| 71 | + if (prices[left] < prices[right]) { |
| 72 | + let profit = prices[right] - prices[left]; // Our current profit |
| 73 | + max_profit = Math.max(max_profit, profit); |
| 74 | + } else { |
| 75 | + left = right; |
| 76 | + } |
| 77 | + right++; |
| 78 | + } |
| 79 | + |
| 80 | + return max_profit; |
| 81 | + }; |
| 82 | + |
| 83 | + const result = maxProfit(prices); |
| 84 | + return ( |
| 85 | + <div> |
| 86 | + <p> |
| 87 | + <b>Input:</b> prices = {JSON.stringify(prices)} |
| 88 | + </p> |
| 89 | + <p> |
| 90 | + <b>Output:</b> {result} |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +#### Codes in Different Languages |
| 98 | + |
| 99 | +<Tabs> |
| 100 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 101 | + <SolutionAuthor name="@Vipullakum007"/> |
| 102 | + ```javascript |
| 103 | + function maxProfit(prices) { |
| 104 | + let left = 0; // Buy |
| 105 | + let right = 1; // Sell |
| 106 | + let max_profit = 0; |
| 107 | + |
| 108 | + while (right < prices.length) { |
| 109 | + if (prices[left] < prices[right]) { |
| 110 | + let profit = prices[right] - prices[left]; // our current profit |
| 111 | + max_profit = Math.max(max_profit, profit); |
| 112 | + } else { |
| 113 | + left = right; |
| 114 | + } |
| 115 | + right++; |
| 116 | + } |
| 117 | + |
| 118 | + return max_profit; |
| 119 | + } |
| 120 | + ``` |
| 121 | + |
| 122 | + </TabItem> |
| 123 | + <TabItem value="Python" label="Python"> |
| 124 | + <SolutionAuthor name="@Vipullakum007"/> |
| 125 | + ```python |
| 126 | + class Solution: |
| 127 | + def maxProfit(self, prices: List[int]) -> int: |
| 128 | + left, right = 0, 1 # Buy, Sell |
| 129 | + max_profit = 0 |
| 130 | +
|
| 131 | + while right < len(prices): |
| 132 | + if prices[left] < prices[right]: |
| 133 | + profit = prices[right] - prices[left] # our current profit |
| 134 | + max_profit = max(max_profit, profit) |
| 135 | + else: |
| 136 | + left = right |
| 137 | + right += 1 |
| 138 | +
|
| 139 | + return max_profit |
| 140 | + ``` |
| 141 | + |
| 142 | + </TabItem> |
| 143 | + <TabItem value="Java" label="Java"> |
| 144 | + <SolutionAuthor name="@Vipullakum007"/> |
| 145 | + ```java |
| 146 | + class Solution { |
| 147 | + public int maxProfit(int[] prices) { |
| 148 | + int left = 0; // Buy |
| 149 | + int right = 1; // Sell |
| 150 | + int max_profit = 0; |
| 151 | +
|
| 152 | + while (right < prices.length) { |
| 153 | + if (prices[left] < prices[right]) { |
| 154 | + int profit = prices[right] - prices[left]; // our current profit |
| 155 | + max_profit = Math.max(max_profit, profit); |
| 156 | + } else { |
| 157 | + left = right; |
| 158 | + } |
| 159 | + right++; |
| 160 | + } |
| 161 | +
|
| 162 | + return max_profit; |
| 163 | + } |
| 164 | + } |
| 165 | + ``` |
| 166 | + |
| 167 | + </TabItem> |
| 168 | + <TabItem value="C++" label="C++"> |
| 169 | + <SolutionAuthor name="@Vipullakum007"/> |
| 170 | + ```cpp |
| 171 | + class Solution { |
| 172 | + public: |
| 173 | + int maxProfit(vector<int>& prices) { |
| 174 | + int left = 0; // Buy |
| 175 | + int right = 1; // Sell |
| 176 | + int max_profit = 0; |
| 177 | +
|
| 178 | + while (right < prices.size()) { |
| 179 | + if (prices[left] < prices[right]) { |
| 180 | + int profit = prices[right] - prices[left]; // our current profit |
| 181 | + max_profit = max(max_profit, profit); |
| 182 | + } else { |
| 183 | + left = right; |
| 184 | + } |
| 185 | + right++; |
| 186 | + } |
| 187 | +
|
| 188 | + return max_profit; |
| 189 | + } |
| 190 | + }; |
| 191 | + ``` |
| 192 | + |
| 193 | + </TabItem> |
| 194 | +</Tabs> |
| 195 | + |
| 196 | +#### Complexity Analysis |
| 197 | + |
| 198 | +- Time Complexity: $$O(n)$$ |
| 199 | +- Space Complexity: $$O(1)$$ |
| 200 | +- Where `n` is the length of the prices array. |
| 201 | + |
| 202 | +</tabItem> |
| 203 | +</Tabs> |
| 204 | + |
| 205 | +:::tip Note |
| 206 | + |
| 207 | +By using a two-pointer approach, we can efficiently solve the Best Time to Buy and Sell Stock problem with a linear time complexity. |
| 208 | + |
| 209 | +::: |
| 210 | + |
| 211 | +--- |
| 212 | + |
| 213 | +## References |
| 214 | + |
| 215 | +- **LeetCode Problem:** [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) |
| 216 | +- **Solution Link:** [Best Time to Buy and Sell Stock Solution on LeetCode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/) |
| 217 | +- **Authors GeeksforGeeks Profile:** [Vipul lakum](https://www.geeksforgeeks.org/user/lakumvipwjge/) |
| 218 | + |
| 219 | +--- |
0 commit comments