Master Data Structures & Algorithms For FREE at AlgoMap.io!
@wilmahutson-e5w5 күн бұрын
This is the best channel hands down. Quick to the point, optimal, simple. Thank you!
@microscorpi0n4 ай бұрын
The hardest part about my approaching this problem is the fact that LC's topic tag is DP. I spent 2+ hours trying to identify the recurrence relationship when this solution was more straightforward. Appreciate that your algomap roadmap categorizes this as Arrays and Strings. I suppose that may have helped with my approach. I didn't realize this problem was in your roadmap until after I attempted it after working on LC's daily challenge from Aug 17, 2024 was LC1937 - Maximum Number of Points with Cost, which suggested the Buy and Sell Stock 1 problem as helpful for recognizing the approach to solving.
@mhmdshaaban3 ай бұрын
It was misleading for me as well. I usually spend some time trying to come up with different solutions, even if I find an efficient one. After I checked the tags and saw DP mentioned, I was like, what the hell is DP doing here?
I understand the solutions, but when you start at 3:00 it would be nice to know the steps that you took in your mind to finding this solution. My challenge is being able to identify the solution
@RapidBeeАй бұрын
I appreciate this, visualizing it helped a lot
@SebastianBeresniewicz23 күн бұрын
What if in the optimal solution your first example started with 2, 8 (And then retained the original values right after)? You wouldn't catch that the max profit would be six because you would later move to use one as your min price and then later capture a Max profit of five wouldn't you?
@CKARlife2 ай бұрын
simple class Solution: def maxProfit(self, prices: List[int]) -> int: min_price = float("inf") maxP = 0 for p in prices: min_price = min(min_price, p) profit = p - min_price maxP = max(profit, maxP) return maxP
@new-anointingaremu35976 ай бұрын
Why do you always use infinity instead of not initializing the variable at all or setting the variable to 0 instead of infinity
@jamestacular6 ай бұрын
If min_price was defaulted to 0 then none of the prices would ever fall underneath it. The point is to set a variable that for certain will be overwritten by the first value (prices[0])
@yairkaz6 ай бұрын
@@jamestacular You can instead initialize min_price to the first variable in the array and in the O(n^2) solution you could have just set max_profit to 0.
@sierraobi3114 ай бұрын
@@jamestacularhow is this true? Say you have an array of practices where all the numbers are non-zero. Why wouldn’t min price be overwritten? Just depends on how you set up the if clause I suppose.
@Infinitely164 ай бұрын
@@sierraobi311 the problem constraints say "0
@_sharkdev_6 ай бұрын
In the brute force solution, is the check for if profit > 0 necessary? I don't work with python, so does its max function have issues when handling zero as an argument?
@AnshuKumar-vr4cv6 ай бұрын
Which language?
@sgetti4dinner6 ай бұрын
Why do we want to keep track of max profit and not max num?
@GregHogg6 ай бұрын
Because that's what we're trying to find
@tuandino69905 ай бұрын
I solved this using dp
@joaoc45085 ай бұрын
could you explain how to use dp here?
@hamadrehman8534 ай бұрын
@@joaoc4508 check neetcode videos. he uses a two pointer approach for this. in neetcode blind 75 series this is the 2nd problem he solves. I came here because his approach was too confusing.