LeetCode 121. Best Time to Buy and Sell Stock [Algorithm + Code Explained ]

preview_player
Показать описание
One of the most frequently asked coding interview questions on Arrays in companies like Google, Facebook, Amazon, LinkedIn, Microsoft, Uber, Apple, Adobe etc.

LeetCode : Best Time to Buy and Sell Stock

Question : Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

Рекомендации по теме
Комментарии
Автор

your way of teaching is amazing...you just delivered your very intuitive thoughts like anything.

shubhamkumar-zntv
Автор

where we can get those slides (hint lists) with approaches that we can take

prataptyagi
Автор

Hello Jayati,
Do you have the unsorted array PPT for every question you sloved on Youtube? I am planning to summary this kind of PPT too. It is very helpful. Thank you:)

fayel.
Автор

Awesome explanation :) Thank you :3
I really like the idea of maintaining solving methods (PPT file) for each data structure.
Could you make a video on how to create something like that for learners?
It would be really helpful. Thanks again :)

SiddyMta
Автор

How to find and return the indices of best day to buy and best day to sell with max profit?

TrungNguyen-eoqz
Автор

Could i have access to the power point? Thanks for the video!

ricardoor
Автор

isn't preprocessing from right to left the max value and then iterating of prices from left and updating a variable ans with max(price[i]-preprocessed Arr[i+1]) and returning max(0, ans) much easier to think about?

arindamthakur
Автор

Please make Best time to buy and sell stock with cooldown Dynamic Programming

code
Автор

Struggled for half an hour and was then able to finally solve it . Feeling good and embarrassed at the same time🙃🙃

uptonogood
Автор

Can you please share your ppt, that is very much helpful, thanks in advance di...

ashishalok
Автор

Please share the link of this ppt, its very useful.

NitinSingh-wplj
Автор

can you please share the link of ppts you are having containing all approaches
plz

vikaspanwar
Автор

Can you share this ppt you created, please. It will be really helpful

ashishjain
Автор

Hi Jayati, could you share those slides?

melvinkimathi
Автор

Why don't you explain the problem in notepad or graphical form. It would be helpful for beginer like us instead just speaking and explaining. ??

prabingupta
Автор

looks like the solution is wrong. For this input [7, 1, 5, 3, 6, 4], solution should be 7, however your code outputs 5

navoriion