LeetCode Best Time to Buy and Sell Stock Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

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

Sure, it is easy to understand solution. But if I am seeing this problem for the first time I am not sure I am able to come up with this. I don't know it is only me or any one else think that way. @Nick - were you able to code this first time you saw this?

pd
Автор

For those wondering, what this algorithm is doing is looking for the smallest number that can be found and then unless there is a smaller number is getting a partial solution, so traversing to get the deep and then getting the highest number after the deep, if a new deep appears then it compares against this new deep and check if there is a greater difference afterwards, always storing the partial solution looking for a better one.
Writing a code solution is worthless unless it has some meaning to understand how to solve the problem and how to face it, and the most important part, the difference between understanding the solution or not is being able to explain it to anyone regardless their knowledge.

logicaymatematicadiscreta
Автор

Why is this problem listed as easy fuck no.

nepalesecreator
Автор

In real -- how to find these questions easy? I just wrote out the 2 for loops answer and then I had no idea HOW to improve the solution.... I just feel no matter how many leet code questions I am seeing... it still isn't getting any better in terms of improvement....

jeezradz
Автор

Nick great video. Though I hate how much you say how easy this problem is, makes me feel dumb when I couldn't think of a way to improve 2 for loops.

jared
Автор

I was watching the Neetcode solution and was so confused. This instantly made sense. Thanks!

anthonyye
Автор

Not a good explanation. You didn't once mention why you did what you did. No mentioned what algorithm you were using. I miss being able to view the down vote button

goofenhour
Автор

great work Nick. One small suggestions can you please categorize your video based on problems type arrays, binary trees, DP, queue etc

vivekkumarsingh
Автор

I set the min_value to the first item, I think that works too. Feel free to point out that won't work.

min_value = prices[0]
max_profit = 0
for price in prices:
if price < min_value:
min_value = price
elif (price - min_value) > max_profit:
max_profit = (price - min_value)


return max_profit

xbeta
Автор

the one problem I was having was that I did not set the minimum value to Int MAX in the declaration.
The point is if you want to keep updating the minimum value, you *have* to start from the biggest possible number and make comparisons that way.
Likewise, if you want to keep updating the maximum value, you have to start from the smallest possible number.

AdrenResi
Автор

Leetcode is just giving "time limit exceeded on my Brute force solution"🤷🏻‍♂️🤷🏻‍♂️

aayushpagare
Автор

im pretty dumb, but i dont get it. does this work even when the last number is the min? I saw a test case like this: [2, 4, 1] so the answer to this would be 2 profit buying on day 1 then selling on day 2, but if the last number is a 1, wouldnt that mess up the min?

honey-xrkp
Автор

Please stop saying how easy it is and that it's a beginner problem. It probably turns some people off (myself included). Other than that, great solution.

JohnnyFive
Автор

Great code Nick! can you please include the Time complexity and space complexity details in all videos

kollurusahithi
Автор

You'd short on day 1, this doesn't take into account the ability to short the market...

lyrana
Автор

Untill today I didn't we could write elseif without else

abhishekgeorge
Автор

Clear and easy to understand. Thank you sooo much.

ahmedouyahya
Автор

Uber asked me this question in an interview but used a list instead, didn't get an offer so here we are lol

Juanszitoo
Автор

Simple and best solution. Can we use stack ?

ravimishra
Автор

Why can't I solve problems like these?

azizagasi