Best Time to Buy and Sell Stock - Leetcode 121 - Arrays & Strings (Python)

  Рет қаралды 15,637

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 22
@GregHogg
@GregHogg 5 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@wilmahutson-e5w
@wilmahutson-e5w 5 күн бұрын
This is the best channel hands down. Quick to the point, optimal, simple. Thank you!
@microscorpi0n
@microscorpi0n 4 ай бұрын
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.
@mhmdshaaban
@mhmdshaaban 3 ай бұрын
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?
@fernandolucas2740
@fernandolucas2740 3 ай бұрын
Totally Agree. Same situation Here.
@ngneerin
@ngneerin 6 ай бұрын
min_price = float('inf') max_profit = 0 for price in prices: min_price = min(min_price, price) max_profit = max(max_profit, price - min_price) return max_profit
@playfuss
@playfuss Ай бұрын
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
@RapidBee Ай бұрын
I appreciate this, visualizing it helped a lot
@SebastianBeresniewicz
@SebastianBeresniewicz 23 күн бұрын
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?
@CKARlife
@CKARlife 2 ай бұрын
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-anointingaremu3597
@new-anointingaremu3597 6 ай бұрын
Why do you always use infinity instead of not initializing the variable at all or setting the variable to 0 instead of infinity
@jamestacular
@jamestacular 6 ай бұрын
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])
@yairkaz
@yairkaz 6 ай бұрын
@@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.
@sierraobi311
@sierraobi311 4 ай бұрын
@@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.
@Infinitely16
@Infinitely16 4 ай бұрын
@@sierraobi311 the problem constraints say "0
@_sharkdev_
@_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-vr4cv
@AnshuKumar-vr4cv 6 ай бұрын
Which language?
@sgetti4dinner
@sgetti4dinner 6 ай бұрын
Why do we want to keep track of max profit and not max num?
@GregHogg
@GregHogg 6 ай бұрын
Because that's what we're trying to find
@tuandino6990
@tuandino6990 5 ай бұрын
I solved this using dp
@joaoc4508
@joaoc4508 5 ай бұрын
could you explain how to use dp here?
@hamadrehman853
@hamadrehman853 4 ай бұрын
@@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.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 758 М.
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
3Sum (Updated Solution) - Leetcode 15 - Two Pointers (Python)
11:11
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 13 М.
Starting a Career in Data Science (10 Thing I Wish I Knew…)
10:42
Sundas Khalid
Рет қаралды 254 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 701 М.
DP 35. Best Time to Buy and Sell Stock | DP on Stocks 🔥
9:11
take U forward
Рет қаралды 437 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 147 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 777 М.
Merge Two Sorted Lists - Leetcode 21 - Linked Lists (Python)
9:41
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН