You are on page 1of 2

Best Time to Buy and Sell Stock

The Solution to LeetCode easy problem

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your pro t by choosing a single day to buy one stock and choosing
a di erent day in the future to sell that stock.

Return the maximum pro t you can achieve from this transaction. If you cannot achieve any pro t,
return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]

Output: 5

Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), pro t = 6-1 = 5.

Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you
sell.

Example 2:

Input: prices = [7,6,4,3,1]

Output: 0

Explanation: In this case, no transactions are done and the max pro t = 0.

Constraints:

1 <= prices.length <= 10^5

0 <= prices[i] <= 10^4

I will share two approaches here to solve the above problem-one is the Brute Force Approach &
the Optimized Approach.

Brute Force Approach

In this for every element traverse the rest part of the array and nd the pro t using prices[j]-
prices[i]; and keep comparing to nd the maximum pro t.

Since for every element you will have to traverse the rest part of the array, the time complexity will
be O(n*n), but the space complexity will remain as O(1).

PS: I have given the logic above, try to code it yourself.💻 ✔

Optimal Approach

In this approach, we will do a linear traversal, our approach would be to nd the smallest element
in the left half and nd the largest element in the other half. Simultaneously I will keep comparing
to nd the maximum pro t.

Now let us do a dry run of the solution to understand it better.

class Solution {

public:

int maxPro t(vector<int>& prices) {

int min_price = INT_MAX; // or take it as 999 anything

int max_Pro t = 0;

for(int i=0;i<prices.size();i++)

min_price = min(min_price,prices[i]);

max_Pro t = max(max_Pro t,prices[i]-min_price);

return max_Pro t;

};

Dry Run : prices = [7,1,5,3,6,4]

min_price = INT_MAX

max_Pro t = 0

min_price = min(INT_MAX,7) = 7

max_Pro t = max(0,1-7) = max(0,-6) = 0

min_price = min(7,1) = 1

max_Pro t = max(0,1-1) = max(0,0) = 0

min_price = min(1,5) = 1

max_Pro t = max(0,5-1) = max(0,4) = 4

min_price = min(1,3) = 1

max_Pro t = max(4,3-1) = max(4,2) = 4

min_price = min(1,6) = 1

fi
ff
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
max_Pro t = max(4,6-1) = max(4,5) = 5

min_price = min(1,4) = 1

max_Pro t = max(5,4-1) = max(5,3) = 5

So therefore the maximum pro t would be 5.

Day of buying stock is 2 i.e prices[1] = 1

Day of selling stock is 5 i.e prices[4] = 6

So maximum pro t is = prices[4] - prices[1] = 6 - 1 = 5

Time Complexity — O(n)

Space Complexity -O(1)

I am sharing some useful resources below :

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

https://www.youtube.com/watch?
v=eMSfBgbiEjk&list=PLgUwDviBIf0rPG3Ictpu74YWBQ1CaBkm2&index=12

Hope you have understood the approach above. In case of any queries feel free to comment
below. Till then keep coding and keep learning, Remember consistency is the key!!!

Since you enjoyed reading my blog , why not buy me a co ee and supoort my work here!! https://
www.buymeaco ee.com/sukanyabharati ☕

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to
our newsletter tinyletter.com/TheLeanProgrammer

fi
fi
ff
fi
fi
ff

You might also like