Тёмный
No video :(

4 Steps to Solve Any Dynamic Programming (DP) Problem 

Greg Hogg
Подписаться 181 тыс.
Просмотров 339 тыс.
50% 1

FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

Опубликовано:

 

23 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 168   
@GregHogg
@GregHogg 5 месяцев назад
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@trusterzero6399
@trusterzero6399 5 месяцев назад
Read this with ypur dirtiest mind
@jjpaq
@jjpaq 5 месяцев назад
2 on 1* tutoring. I think that's why they call it "pair programming". 😉
@conundrum2u
@conundrum2u 5 месяцев назад
reported for advertising
@xNothing2Lose
@xNothing2Lose 4 месяца назад
Ty for that great DP help. Couldn't do it alone. No matter how much I put into that.
@Skubidi-qy8hb
@Skubidi-qy8hb 2 месяца назад
How much money will I need for tutoring from you?
@rohitkumaram
@rohitkumaram 5 месяцев назад
4th step sometimes not possible, but yeah up to 3 definitely.
@GregHogg
@GregHogg 5 месяцев назад
Yes that's correct
@iiJDSii
@iiJDSii 5 месяцев назад
When is the 4th step not possible I wonder? Maybe when the DP transition formula involves variable time step difference or something like that (i.e. something more dynamic that dp[i-1] and dp[i-2])
@rikthecuber
@rikthecuber 5 месяцев назад
​@@iiJDSiiThere are tons of problems where the space cannot be optimised to lower dimensions. It is basically when the value of a paticular entry in dp table doesnt depend on a predictable element, or maybe different values depend on different entries spanning the entire dp table. In the fibo case, we could already say that the value at the current position depends on just the last 2, so we could optimise the dimension down to 0.
@Dawid_NH
@Dawid_NH 17 дней назад
@@iiJDSii for example: 322. Coin Change
@sonicjoy2002
@sonicjoy2002 2 месяца назад
very good summary of the step by step optimisation of DP problems, but it is hard for new learner to understand these 4 steps without a full blown explanation.
@yassinesafraoui
@yassinesafraoui 3 месяца назад
What's complicated about DP IMO is figuring out what do the output depend on in such a way that it's memoizable, when facing new and complicated problems, It's not obvious at all
@GregHogg
@GregHogg 3 месяца назад
Yes that's definitely the tricky part
@Karthik-h3r
@Karthik-h3r 19 дней назад
What do DP do?: It actually selects one best solution from all the possibilities And the possibilities are reduced by reusing/Memorization/Tabulation,(when there are 2 or more subproblems overlapping then we can avoid those subproblems and substitute from the memorization 😎)
@azursmile
@azursmile 5 месяцев назад
Great video demonstrating progressive code refinement. Still not sure if the first step should be called "recursive backtracking". As discussed in previous video, think this is just "naïve recursion".
@Samuel0202
@Samuel0202 5 месяцев назад
Just throw a HashMap on it
@thisisnotok2100
@thisisnotok2100 5 месяцев назад
That's step 2 yes
@m96fa40
@m96fa40 5 месяцев назад
You didn't understand the second step lol
@IvanRYTP
@IvanRYTP Месяц назад
guys, it's a thing Nicolas T. said in one of his videos lol
@RealCGH
@RealCGH Месяц назад
That's #2😂, it's a dictionary in Python but same exact thing
@Samuel0202
@Samuel0202 Месяц назад
OMFG, it's an inside joke in the programming community 🙄. No wonder you all don't get it when you watch RU-vid tutorials.
@abiiranathan
@abiiranathan 5 месяцев назад
I think the memo dict would be a new one for each stack frame. I might be wrong. You are better off using lru_cache from functools or make the dict global
@Gamiboi612
@Gamiboi612 2 месяца назад
I was confused as well. How would it work if each time fib is called, the memo object is reset?
@del6553
@del6553 11 дней назад
The memo dict was instantiated inside fib(n) and its reference was stored in the name "memo"; The f(n) function calls itself, not fib(n). Python uses the LEGB (Local, Enclosing, Global, Built-in) rule for name resolution, when it look up the name "memo" within f(n) and find out that it's not there, it will look for that name in the enclosing scope aka fib(n). Since there are only lookups and no assignment to the name "memo" within f(n), the "memo" within f(n) is the essentially the same as "memo" in fib(n).
@88Xrist
@88Xrist 3 месяца назад
I find that top-down solutions are easier to understand due to the decision tree nature of the solution as opposed to the bottom-up with dp that is sometimes harder to find the transition step.
@GregHogg
@GregHogg 3 месяца назад
I completely agree 💯
@tomislam
@tomislam 4 месяца назад
Hey Gregg, I'd love to see a deep dive into DP. Most importantly, I'd like to know how I can convert my naive approach progress from top-down to bottom-up to true dp solutions. Thanks in advance.
@GregHogg
@GregHogg 4 месяца назад
Thank you! Yes I am absolutely working on those longer form videos to discuss these topics :)
@balasujithpotineni8184
@balasujithpotineni8184 5 месяцев назад
I feel like you just summed up one the first lecture on dynamic programming from an old mitocw series
@xingyuxiang1637
@xingyuxiang1637 5 месяцев назад
Just think about the years it takes to summarize this classical intro DP problem. Another thing is to distinguish greedy and DP because they both ask for min/max and get results based on the previous results. So, I would stop at the Top-down DP and draw some trees due to @cache. I would use return at the leaf node.
@kakimell101
@kakimell101 5 месяцев назад
I thought I was good with Python until I saw this video
@polycrystallinecandy
@polycrystallinecandy 5 месяцев назад
This isn't Python though, it's basic algorithms :)
@kakimell101
@kakimell101 5 месяцев назад
@@polycrystallinecandy it's definitely python
@rikthecuber
@rikthecuber 5 месяцев назад
This video has nothing specific to do with python.
@kakimell101
@kakimell101 5 месяцев назад
Pretty sure I saw python
@polycrystallinecandy
@polycrystallinecandy 5 месяцев назад
​@@kakimell101Pretty sure I also saw vscode, doesn't mean it's got anything to do with it
@petipeti2997
@petipeti2997 5 месяцев назад
this is gold thank you
@GregHogg
@GregHogg 4 месяца назад
You're super welcome, sorry for the slow response!
@michalr2284
@michalr2284 23 дня назад
Step 5, make it faster by using fast matrix exponentiation.
@ravimishra3169
@ravimishra3169 25 дней назад
Gernalized an example for whole dp ,hence incorecct
@foerza649
@foerza649 5 месяцев назад
How to do the 4th step in antoher algorithm? For example the knapsack?
@GregHogg
@GregHogg 4 месяца назад
It's just about storing exactly what you'd need to store at each time, and this will vary per problem
@carlgauss8545
@carlgauss8545 5 месяцев назад
5th solution, solve it on O(ClogN) where C depends on the dp specific recursion
@paw565
@paw565 9 дней назад
So basically for loop is better than recursion 😊
@sergeiilin544
@sergeiilin544 4 дня назад
Backtracking solution indeed o(2^n) but there is tighter upper bound
@sponge3757
@sponge3757 5 месяцев назад
and 5 step use matrix
@YuvrajRaghuvanshiS
@YuvrajRaghuvanshiS 3 месяца назад
The ting sound resonated my soul
@GregHogg
@GregHogg 3 месяца назад
Good good 😂
@sarthakjain1824
@sarthakjain1824 Месяц назад
I can usually think of a top down approach but not the bottom up
@abdalla4425
@abdalla4425 5 месяцев назад
#4 should be optimized space complexity which includes O(1) for 1D DP instead of O(n) and O(n) for 2D DP instead of O(n^2)
@GregHogg
@GregHogg 4 месяца назад
You cannot guarantee this unfortunately!
@mathtsai899
@mathtsai899 4 месяца назад
Too complex. Just learn the formal dp from Algorithm book. I suggest "Introduction to algorithm III". If there are overlapping subproblems and optimal substructure, then we can use dp. Both top-down and bottom-up dp are tabulation. (This vedio is not right.) Tabulation means memorize the result to calculate everything in only 1 time. Here are the steps to do dp problems: 1. Define the definition of your dp. Ex. dp[i] means the minimum step to reach ith floor. 2. Write down the recursion. Ex. dp[i] = dp[i-1]+dp[i-2] in Fibonacci. 3. Write down boundry conditions. Ex. n = ?, then... Hope this can help someone learning dp. : )
@HeatCode24
@HeatCode24 3 месяца назад
Just for folks. 4th is not greedy approach. It’s still a dp based.
@GregHogg
@GregHogg 3 месяца назад
Yes still dp just constant space where possible. Feels kinda greedy though
@markokafor7432
@markokafor7432 25 дней назад
For the last option, you didn’t use the i in the for loop?
@nihalbhamrah4726
@nihalbhamrah4726 Месяц назад
I dint understand 2nd one but other 3 were still understanable
@armandomendivil1117
@armandomendivil1117 5 месяцев назад
If we want to go little further we can take a look at the Golden ratio
@joergsonnenberger6836
@joergsonnenberger6836 5 месяцев назад
Yeah, why iterate when you can directly compute the result? :)
@carlkarl
@carlkarl 4 месяца назад
i dont know about the caching thing thanks for telling and explaining
@GregHogg
@GregHogg 4 месяца назад
Yeah, pretty interesting stuff! You're very welcome :)
@eduardprivat9821
@eduardprivat9821 5 месяцев назад
How perfect. 4h@uni vs your smart and small overview explenation. Thx
@ashraf_isb
@ashraf_isb 5 месяцев назад
thanks man
@Allthenameable
@Allthenameable 4 месяца назад
To solve dp you should solve dp 3 times. ..got it
@GregHogg
@GregHogg 4 месяца назад
At least the first couple times you learn about the concept, yes I believe so :)
@augustacybersolutions
@augustacybersolutions 4 месяца назад
how is example 1 "recursive backtracking" when it just produces the nth number of a fibonacci sequence?
@GregHogg
@GregHogg 4 месяца назад
Because it does it through recursive backtracking, or basically, the most brute force thing possible
@augustacybersolutions
@augustacybersolutions 4 месяца назад
@@GregHogg is that because it starts at f(6) and basically does a trickle down tree (horrible description) and then when it gets to the default cases of 0 and 1 it goes back up the tree to the actual f(6)? Sorry just trying to work through the terminology, i'm new to studying actual algorithms with programming
@ishaanbhardwaj2313
@ishaanbhardwaj2313 Месяц назад
Is 4th step limited to 1-D dynamic programming
@ishaanbhardwaj2313
@ishaanbhardwaj2313 Месяц назад
?
@anoxyt3140
@anoxyt3140 3 месяца назад
Can u do this in C++
@PaulSebastianM
@PaulSebastianM Месяц назад
What is considered as DP here!?
@elirane85
@elirane85 3 месяца назад
Just remember: Dynamic Programming = Recursion + Memoizations
@danielniels22
@danielniels22 4 месяца назад
hi guys, im new in programming. on the 2nd approach, even though it results better in time complexity (lowered to O(n) ), doesn't it affect performance on the space complexity because it does memoization?
@GregHogg
@GregHogg 4 месяца назад
Correct. Although we usually care more about time :)
@danielniels22
@danielniels22 4 месяца назад
@@GregHogg thank you!
@aertyty3900
@aertyty3900 5 месяцев назад
D&C, cht, lambda optimization, optimizations using fft entered the chat...
@blasterzm
@blasterzm 3 месяца назад
You totally missed the point of DP. You can pass just by explaining how you derive the solution and implement bottom up or top down with memo, which ever is easier for you
@natescode
@natescode 4 месяца назад
Recursion uses O(n) memory though
@GregHogg
@GregHogg 4 месяца назад
Indeed it does!
@samgraham6355
@samgraham6355 4 месяца назад
Step 5: Binet's formula and rounding
@GregHogg
@GregHogg 4 месяца назад
What's that?
@hepotitus
@hepotitus 5 месяцев назад
me* watches how to solve dynamic programming problems also me* messes up f-strings
@captainrex5457
@captainrex5457 2 месяца назад
Or just us the formula in O(1)…
@phusicus_404
@phusicus_404 2 месяца назад
It's not steps, it's ways
@GregHogg
@GregHogg 2 месяца назад
Sometimes true. Agreed, you don't actually have to follow these steps if you know what you're doing. I'd argue it's a natural order to solving the problem, understanding it completely, and getting good practice in
@mostafagalal1584
@mostafagalal1584 3 месяца назад
I started studying programming for data science about two months ago and what h r saying sounds like Chinese to me, am soooo far 😂
@GregHogg
@GregHogg 3 месяца назад
Dynamic programming shares pretty much nothing in common with data science (although sadly still you might get asked it in an interview), so that's okay haha
@saltedskin
@saltedskin 3 месяца назад
I wonder if people having passed the dynamic-programming tests ever actually used it in their jobs?
@GregHogg
@GregHogg 3 месяца назад
They definitely don't 😂
@faizalahmed648
@faizalahmed648 3 месяца назад
Why not use a hashmap?
@GregHogg
@GregHogg 3 месяца назад
For the memoization?
@mdmnbp1183
@mdmnbp1183 5 месяцев назад
cool
@piyusharora5327
@piyusharora5327 5 месяцев назад
It might be fun if I am employed by Netflix though.
@calebclay2713
@calebclay2713 2 месяца назад
Anyone lost or just me
@denysdanov88
@denysdanov88 5 месяцев назад
Good example but fibonacci could be solved with O(1) using math
@ismailseif6945
@ismailseif6945 5 месяцев назад
precision issues will start to appear, also what if I want it modulo a large prime (like 10^9 + 7) and ask you to find the, for example, 748243191942148th Fibonacci number
@nikolatesla3376
@nikolatesla3376 5 месяцев назад
impossible, logN runtime minimum
@xxsuper99xx
@xxsuper99xx 5 месяцев назад
Show me the cpu that has a hardware integrated square root function. N64 doesn't count
@natescode
@natescode 4 месяца назад
​@@nikolatesla3376no it isn't impossible. I did it on Leetcode. Unfortunately it depends on having an infinity accurate value for the golden ratio.
@DavideCanton
@DavideCanton Месяц назад
​@@xxsuper99xx Square root of 5 can be precomputed and amortized, and it's not dependent on N
@revathivamshi7621
@revathivamshi7621 5 месяцев назад
Does Devin will take over the jobs
@thecoolnewsguy
@thecoolnewsguy 4 месяца назад
No
@MdAshfakChowdhury
@MdAshfakChowdhury 3 месяца назад
🎁✅
@GregHogg
@GregHogg 3 месяца назад
Did you send me a gift haha
@michaeltruong405
@michaeltruong405 5 месяцев назад
W
@InvictusM0n
@InvictusM0n 3 месяца назад
Bro i can barely solve stacks medium. I am truly cooked.
@GregHogg
@GregHogg 3 месяца назад
You'll continue to improve, give it time 😄
@gigantopithecus8254
@gigantopithecus8254 5 месяцев назад
the fastest method doesent use dp though
@Dd-do-and-dont
@Dd-do-and-dont 3 месяца назад
I spotted a trash, it is at the beginning, it resembles 🍎
@GregHogg
@GregHogg 3 месяца назад
What?
@Dd-do-and-dont
@Dd-do-and-dont 3 месяца назад
@@GregHogg I mean that logo in the center belonging to company engaged in slave labor in china.
@keepNjoying
@keepNjoying 4 месяца назад
Is coding relevant in this AI era?
@GregHogg
@GregHogg 4 месяца назад
Yes
@aheensolanki8956
@aheensolanki8956 Месяц назад
Why is CDawgVA teaching coding😭?
@Apeuda
@Apeuda 20 дней назад
chatgpt ahh content
@TheAcebyte
@TheAcebyte 5 дней назад
Ungrateful brat
@AllanSavolainen
@AllanSavolainen 5 месяцев назад
How is this dynamic programming? Looks like normal programming to me... 😅
@GregHogg
@GregHogg 4 месяца назад
Haha well it is still programming just a certain type of it
@AllanSavolainen
@AllanSavolainen 4 месяца назад
@@GregHogg sure sure, but I can't understand how it is anything different from normal programming where you divide the task into small sub tasks and solve those.
@perenegro
@perenegro 4 месяца назад
Damn. I didn't understand a shit. Now I feel bad.
@danielniels22
@danielniels22 4 месяца назад
xD
@GregHogg
@GregHogg 4 месяца назад
60 seconds is a pretty short amount of time to learn 4 algorithms. This is a high level understanding video, it'll likely take more time than this to fully grasp :)
@BLMed1a
@BLMed1a 2 месяца назад
Personally don’t like this approach I think learning straight the way to dp with an array is the correct way, this is how we’re thought in the University The other are good ways to solve general problems, but not DP ones
@slyace1301
@slyace1301 5 месяцев назад
I wrote c#, i don't know if it can be done any other way than just using recursion with a limiting condition without making the code look like gibberish
@Ashwinnbr
@Ashwinnbr 5 месяцев назад
5th step cry
@diegotapiasilva7349
@diegotapiasilva7349 4 месяца назад
Had no clue what u just said
@iSaac-kp5lk
@iSaac-kp5lk 5 месяцев назад
Yeahh... potato potato
@yomajo
@yomajo Месяц назад
Big words, no listen
@GPSingh-tn3zo
@GPSingh-tn3zo 3 месяца назад
5th solution - i think you are lagging.
@GregHogg
@GregHogg 3 месяца назад
Hmm?
@yaroslavpanych2067
@yaroslavpanych2067 5 месяцев назад
Fancy term you use "Dynamic Programming". Just solve fucking task, do not waste time memorising this shit.
@polycrystallinecandy
@polycrystallinecandy 5 месяцев назад
Why solve the problem 4 times? Just write down the DP formulation and then write the code. Also step 4 is just for special cases. You don't know if the solution will depend on a constant number of previous values.
@TheStrandedAlliance
@TheStrandedAlliance 5 месяцев назад
What do you mean, step 4 is just for special cases? When wouldn't it work?
@polycrystallinecandy
@polycrystallinecandy 5 месяцев назад
​@@TheStrandedAlliance Like I said, when the current value doesn't depend on a constant number of previous values. Or when you don't know which values to maintain in advance. In the simple case of the fibonacci series, you only have to save exactly 2 values regardless of the current step, and you know which ones. In more complex problems you won't know in advance which values to save or how many. So you can't allocate O(1) memory for them and the only way is to maintain the whole table.
@TheStrandedAlliance
@TheStrandedAlliance 5 месяцев назад
@@polycrystallinecandy Can't you just manage a dynamic list then? Also, in the recursive case, the values will just fill up the stack instead anyway, no? That would be pretty bad if you have a huge amount of values (-> stack overflow).
@polycrystallinecandy
@polycrystallinecandy 5 месяцев назад
@@TheStrandedAlliance no, because you need to know ahead of time. By the time you get to the iteration where you know which values you should've saved, you've already lost them. You don't have to do recursive, you can do an iterative version (step 3), just save all the values in a table.
@TheOtherSteel
@TheOtherSteel 5 месяцев назад
"...there is four specific steps..." Should be: "...there are four specific steps..."
@hockeymikey
@hockeymikey 5 месяцев назад
Imagine studying just for a job.
@GregHogg
@GregHogg 5 месяцев назад
Imagine not studying just for a job
@hockeymikey
@hockeymikey 5 месяцев назад
@@GregHoggDidn't study in college, not gonna start now and I was summa cum laude.
@red_991
@red_991 5 месяцев назад
@@hockeymikeyNobody cares
@chillfill4866
@chillfill4866 5 месяцев назад
He has a point😂 you should have figured it out in college!
@user-pk3jr1ng6h
@user-pk3jr1ng6h 5 месяцев назад
​@@GregHogg😂😂cool man,BTW thanks for the video
@itsmaxim01
@itsmaxim01 5 месяцев назад
…or just write it in C and actually get somewhat decent performing programs.
@thefunnybuddy4138
@thefunnybuddy4138 16 дней назад
f-a-apple-n-g fappling
Далее
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Просмотров 181 тыс.
Avaz Oxun - 10 yillik yubiley konsert dasturi 2023
2:52:33
Fast Inverse Square Root - A Quake III Algorithm
20:08
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Internet is going wild over this problem
9:12
Просмотров 149 тыс.
0/1 Knapsack problem | Dynamic Programming
13:29
Просмотров 150 тыс.
The LeetCode Fallacy
6:08
Просмотров 479 тыс.
Avaz Oxun - 10 yillik yubiley konsert dasturi 2023
2:52:33