Minimum path sum | Min cost Path | Dynamic programming | Leetcode #64

Поділитися
Вставка
  • Опубліковано 24 січ 2025

КОМЕНТАРІ • 234

  • @codestorywithMIK
    @codestorywithMIK 4 роки тому +199

    Explaining things like "Why won't Greedy work here", then going from "recursive" approach to optimized DP approach. This is how everyone wants to see a problem and this is how everyone should look at a problem. Start from worst and make improvements step by step. This makes your teaching style special. Thanks

  • @ronaldabellano5643
    @ronaldabellano5643 4 роки тому +30

    The best. I like the use of colors that made it easy to follow.

  • @abcdefg1740-b3j
    @abcdefg1740-b3j 4 роки тому +9

    I come here whenever I start to feel anxious about my solution not panning out, and I'm solving in Python. :) Thanks again for these walk-throughs, they really help. =D

  • @CostaKazistov
    @CostaKazistov 2 роки тому +1

    Clearest explanation of 3 different approaches to solving Minimum Path Sum problem 💯

  • @alfredomora5555
    @alfredomora5555 3 роки тому +6

    I just had an assessment wish I saw this video earlier. The question was very similar. I appreciate your content thank you!

  • @ismail8973
    @ismail8973 3 роки тому

    Explanation for Why greedy algorithm doesn't work was top notch. people usually doesn't try to think or explain in that angle. Thanks

  • @zelo_s6707
    @zelo_s6707 3 роки тому +1

    Thanks for this approach! Using the greedy and recursive approaches helped me understand why one would use the DP method. Nothing else made sense up until now. Keep it up!

  • @peter-eh2oq
    @peter-eh2oq 2 роки тому

    Thanks! Please keep going!!! Thanks soooooooooo much!!! I saw a lot of vedio about this question, you have the best explination.

  • @Noizept
    @Noizept 4 роки тому +4

    The best video i found so far to explain this problem.

  • @anushreegupta5830
    @anushreegupta5830 2 роки тому +2

    Hats off to your teaching style! Legit the best explaination.

  • @techwithlord
    @techwithlord 3 роки тому +1

    This is a best ever video I have ever seen from this only one video I clearly understand the GREEDY, RECURSION AND BACKTRACKING also hats off dude to your work

  • @dileepkumar-nd1fo
    @dileepkumar-nd1fo 3 роки тому +3

    You are mastered in explaining the things man.
    Such a neat explanation with clear voice and the colour combination will make us remember easy.

  • @khan.mansoor
    @khan.mansoor 3 роки тому +1

    You explain both problem and solutions very well. Commenting for expressing gratitude as well to offer support. Please keep up the good work.

  • @PiyushKumar-ey7qw
    @PiyushKumar-ey7qw 4 роки тому +7

    memoization approach did work like a charm, instead of a 2D array, I have used unordered_map where key was a pair of i,j value.
    It got accepted.

  • @indsonusharma
    @indsonusharma 4 роки тому +4

    i did by marking technique (iterative) like if you have not encountered then add that state and mark it but if you have encountered that (check by using mark 2d array )state before then take minimum of previous and current state(bit mathematical) that also lead me to AC but thanks for this beautiful approach bro! today the base case could'nt stop us in the first go lol!!

    • @techdose4u
      @techdose4u  4 роки тому

      This concept is same as memoization. Today I already wrote the base case 🤣

  • @HimanshuKumar-sl6ud
    @HimanshuKumar-sl6ud 3 роки тому +3

    the way you explain complex things in easy way is awesome. please make more problems solution of leetcode .

  • @prathamkesarkar
    @prathamkesarkar 3 роки тому +1

    As usual sir I was able to solve this within minutes with your explanation.

  • @debaratighatak2211
    @debaratighatak2211 3 роки тому +4

    Another amazing explanation, I can't really thank you enough sir! I :) The bright colours in the video really helps a lot too

  • @Dizzydizzydizzy7
    @Dizzydizzydizzy7 2 роки тому

    Literally best explaination on youtube

  • @Rasheed21
    @Rasheed21 3 місяці тому

    That's incorrect at 7:18; memoization significantly improves time. For a 15x16 grid I was able to reduce recursive calls from 445,962,869 to 479 where there were 209 cache hits. The time dropped from 1347 ms to 1ms. That's a 1347 times improvement on time and nearly 100,000 times improvement on recursive calls. This was with Java using a 2D array for the cache (i.e. memoization) instead of HashMap. The time spent filling the array with -1's had little impact. Using HashMap also had good time of 7 ms with the same improved numbers for recursive calls and cache hits.

  • @simon7671
    @simon7671 3 роки тому +3

    you help me a lot! I am a beginnner and this question is just too hard for me. After watching your video, i am able to finish a similar question by myself!

  • @rishabhgoyal8110
    @rishabhgoyal8110 4 роки тому +4

    I hope this channel reaches 1m soon!

    • @techdose4u
      @techdose4u  4 роки тому +2

      Thanks for your support 😀

    • @Daniel_WR_Hart
      @Daniel_WR_Hart 3 роки тому

      LeetCode only has a bit over 200K users with a contest ranking, so that might take a while

  • @DrJavyMartinez
    @DrJavyMartinez 3 роки тому +1

    Dude! Your accent is tough, but after watching your video, I feel EXTREMELY comfortable applying minimum and maximum cost sum traversal algorithms through graphs!!

    • @techdose4u
      @techdose4u  3 роки тому +1

      Great :) Sorry for this accent though 😅

  • @HariPrasad-ox5ri
    @HariPrasad-ox5ri Рік тому +1

    Thank you for the simple and elegant explanation!

  • @utkarsh5165
    @utkarsh5165 Рік тому

    Wonderful Explanation BY Tech Dost.. (Dosh)

  • @prafulparashar9849
    @prafulparashar9849 2 роки тому

    Great explanation !!
    The steps which we followed were the exact steps and approaches which came to my mind while looking at this problem for the very first time.
    I was able to go till recursion(backtracking) approach but could not figure out the last one.
    Thanks !! This was helpful.

  • @ayoubdkhissi
    @ayoubdkhissi 3 роки тому +3

    this explanation couldn't be better!! thanks

  • @fussycat2798
    @fussycat2798 3 роки тому +1

    Nicely explained the concept. Really like it. I was able to solve the similar problems in less time. Thanks a lot for the detailed explanation

  • @PriyanshuKumar-om8np
    @PriyanshuKumar-om8np 2 роки тому

    The best explanation I have ever seen.

  • @AltafHussain-on2oe
    @AltafHussain-on2oe 3 роки тому +2

    I am falling in love with your Explanation 💖

  • @prashantagrawal1140
    @prashantagrawal1140 3 роки тому +1

    Brilliant Explaintion techdose

  • @viralmehta2542
    @viralmehta2542 4 роки тому +2

    Nice video. Covered various approaches here.

  • @UJJWALGIRIRA
    @UJJWALGIRIRA 2 роки тому

    bhai op zeher explaination🔥🔥🔥🔥🔥🔥🔥

  • @programmingstuff5741
    @programmingstuff5741 4 роки тому +6

    Can you post the solutions of Google kickstart Round A and Round B
    These are the such good problems to practice

  • @ambujverma9287
    @ambujverma9287 4 роки тому +1

    the way you explain things is phenomenal...
    thanks a lot

    • @techdose4u
      @techdose4u  4 роки тому

      Welcome :)

    • @ambujverma9287
      @ambujverma9287 4 роки тому +1

      @@techdose4u bhai you work in any MNC or u study in college(iit?)

    • @techdose4u
      @techdose4u  4 роки тому

      MNC....

    • @ambujverma9287
      @ambujverma9287 4 роки тому

      @@techdose4u which one.. if you don't mind brother 😅

  • @wilfredomartel7781
    @wilfredomartel7781 2 роки тому +1

    Great!

  • @AnshumanSharmadev
    @AnshumanSharmadev 4 роки тому +2

    Amazing!! You made it look so simple! Liked and Subscribed!

  • @pradeepvdeepu4768
    @pradeepvdeepu4768 3 роки тому +2

    awesome explanation and smooth ..... ! thanks a lot

  • @shivanij3725
    @shivanij3725 4 роки тому +2

    nice explanation about dp table.Thanks

  • @alancabrera7116
    @alancabrera7116 4 роки тому +1

    iiuc, the space complexity is O(n). One only needs one vector for the running minimums.

    • @techdose4u
      @techdose4u  4 роки тому +1

      True. 2 maintaining 2 rows is sufficient. This is true for most table type DP programs.

  • @InsightWeaver
    @InsightWeaver 2 роки тому

    Amazing video mate! explained thoroughly. Cheers

  • @ABHISHEK-fy1in
    @ABHISHEK-fy1in 4 роки тому +1

    Very well explained. 👌

  • @OP-yw3ws
    @OP-yw3ws Рік тому

    Amazing you solved all my doubts

  • @Safar_Ayushi_Ka
    @Safar_Ayushi_Ka 4 роки тому +2

    Your videos are amazing plz do make more videos on DP ,it would be grateful Thanks

  • @neharikakhera6411
    @neharikakhera6411 4 роки тому +3

    Thank you so much for great explanation!!

  • @nabidhasan9548
    @nabidhasan9548 2 роки тому +1

    Great explanation brother

  • @shreelakshmi6890
    @shreelakshmi6890 3 роки тому

    Great Explanation!

  • @amansarma417
    @amansarma417 4 роки тому +6

    i did without the dp vector. Since we could just perform modifications on the same given grid matrix.
    Space Complexity O(1) :) . I submitted that way

  • @lemax2980
    @lemax2980 4 роки тому +3

    Great sir, as usual😋

  • @shapedsilver3689
    @shapedsilver3689 3 роки тому +3

    When he started getting into why greedy algorithms won't work I knew this was legit

  • @paragroy5359
    @paragroy5359 3 роки тому +1

    Nice explanation sir....but instead of taking an additional dp vector if we do changes in the given grid vector only then it will be o(1) space solution

  • @Somethingsomethinglol
    @Somethingsomethinglol 3 роки тому +2

    Excellent!

  • @jameskurakula8560
    @jameskurakula8560 3 роки тому

    @7:38
    grid[I][j]+ min( dp[ i-1][j], dp[I][j-1]) , there it was j-2.

  • @openworld7585
    @openworld7585 4 роки тому +1

    Thank you for awesome explanations of dp table

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt 4 роки тому +1

    great video sir...........:)

  • @harifrahman8054
    @harifrahman8054 4 роки тому +1

    Nice Explanation

  • @hiteshusingh8571
    @hiteshusingh8571 4 роки тому +1

    very nice explanation

  • @tekbssync5727
    @tekbssync5727 3 роки тому +4

    What a color combination and style of *Leet code* in the thumbnail ? !!!! 😂

  • @chelseaip759
    @chelseaip759 4 роки тому +1

    THE BEST TUTORIAL

  • @subratarudra2745
    @subratarudra2745 Рік тому

    Nice explanation sir

  • @kercat89
    @kercat89 4 роки тому +2

    It's so easy to understand and I loved the break down from the simple to sophisticated approach!
    I have a question re the recursive solution - why is the complexity O(2^n) and not O(2^(m*n))? if it means that there's 2 options to about each cell..? Thank you

    • @techdose4u
      @techdose4u  4 роки тому +3

      Thanks. Actually our number of steps will always be (m+n) from top left to bottom right. So, you can say time will be 2^(m+n).

    • @kercat89
      @kercat89 4 роки тому +1

      @@techdose4u That makes sense. Thank you!

  • @anandakrishnan2685
    @anandakrishnan2685 3 роки тому

    Nice explanation, thanks!

  • @imuksd
    @imuksd 3 роки тому

    Good Explanations!!

  • @blume0f
    @blume0f 4 роки тому +5

    awesome sir

  • @rahulbhangale4656
    @rahulbhangale4656 3 роки тому

    greedy works if you properly define dp table. dp[i][j] denotes minimum path sum from (1,1) to (i,j)

  • @shaileshhegde9205
    @shaileshhegde9205 4 роки тому

    The recursive way is DFS right with directions of right and below

  • @lashalezhava2313
    @lashalezhava2313 4 роки тому +2

    Thanks, you explain very well :)

  • @sarvagyaiitmadras8727
    @sarvagyaiitmadras8727 3 роки тому +1

    if all 4 direction mvement would be allowed then what would be changes, will changes work in it, or completenew approach would be needed ???

  • @jeromealve6321
    @jeromealve6321 3 роки тому +3

    I wish this video came out 3 years ago when I took my algorithms class lol

  • @amitavamozumder73
    @amitavamozumder73 3 роки тому +1

    we can also do dijksta here right? the value in each cell can be the edge weight, which is pretty close to the dp solution.

    • @techdose4u
      @techdose4u  3 роки тому +1

      You can

    • @amitavamozumder73
      @amitavamozumder73 3 роки тому

      @@techdose4u can you please make a video on myers diff algorithm, (the one used in github file diff), it's not anywhere in youtube,

  • @mohammedsadiq1567
    @mohammedsadiq1567 4 роки тому +2

    Awesome bro!!

  • @ritikgandhi3605
    @ritikgandhi3605 4 роки тому +2

    In line 11 of the code ,why did we initialize cols as
    int cols=grid[0].size() and not just as grid.size()?

    • @techdose4u
      @techdose4u  4 роки тому +1

      Because the number of cols are same in each row and grid[O] means no of cols. Grid.size means no of rows because it is a 2D matrix.

  • @rushabhlegion2560
    @rushabhlegion2560 Рік тому

    What is that Fast I/O in C++ line at the top? Is it necessary?

  • @programmingstuff5741
    @programmingstuff5741 4 роки тому +1

    Can you post videos on segment tree??
    Which is very useful for the purpose of comptetive programming

    • @techdose4u
      @techdose4u  4 роки тому

      Already done. Search in videos.

  • @moonbrush5
    @moonbrush5 2 роки тому

    I am stuck with a similar problem which is finding shortest path in 2D matrix from source to target which given in the problem and I have to return all i,j coordinates of the shortest path only. Please help.

  • @jaredmomanyi8178
    @jaredmomanyi8178 Рік тому

    works like magic

  • @jeevanr5814
    @jeevanr5814 3 роки тому

    Dp approach is not working for some testcase can you please check it

  • @pallabghosh3840
    @pallabghosh3840 2 роки тому

    can i say the time will be in recursive approach 2^(m*n) in worst case? so we use dp for n^2 time

  • @veereshr
    @veereshr 3 роки тому +1

    if we can change the input matrix, we can use the same right? memory optimisation?

    • @techdose4u
      @techdose4u  3 роки тому

      Yes if the array is mutable

  • @akanshagautam9815
    @akanshagautam9815 4 роки тому +3

    If we can move up, down, left, and right then what changes do we have to make in the code?

    • @techdose4u
      @techdose4u  4 роки тому +4

      Maybe try with Dijkstra graph algorithm.

    • @harshitchoukse2602
      @harshitchoukse2602 3 роки тому

      Then you need track the path with visited array in order to bypass the cycle.

  • @lifestoriesfromearth6271
    @lifestoriesfromearth6271 3 роки тому

    which tool are you using for whiteboard

  • @raj.saurabhh
    @raj.saurabhh 3 роки тому

    Can we not use the extra space and use the given table for dp? I mean my testcase is ok but when i submit one solution is wrong

  • @praveennandham2933
    @praveennandham2933 4 роки тому

    can we change the same logic ass required
    when soure and destination or given??

  • @alexsparrow8614
    @alexsparrow8614 4 роки тому +1

    can you tell boundary check condition in recursive Method.....

    • @techdose4u
      @techdose4u  4 роки тому

      Out of bounds. Reaching the desired cell. Reject higher cost path (if using memoization).

  • @LovelyMountainGoat-rd7qy
    @LovelyMountainGoat-rd7qy Рік тому

    Thank you😇

  • @mohitsoni3446
    @mohitsoni3446 4 роки тому +1

    awesome

  • @vedant9887626061
    @vedant9887626061 4 роки тому +1

    What will be the boundary conditions for recursive approach

  • @shivanggupta5421
    @shivanggupta5421 4 роки тому

    def findPath(x:int,y:int):
    if(x= len(grid) or y=len(grid[0])):
    return
    right = findPath(x,y+1)
    down = findPath(x+1,y)
    return min(right,down)+grid[x][y]
    I used this method. I know you said recursive method won't work but I tried for my satisfaction. But when compiling it gives error because it isn't able to compare right and left when the boundary check returns null. Can you tell me where am I wrong

  • @gopalmishra3527
    @gopalmishra3527 3 роки тому

    Code doesn't work for multiple testcases on gfg

  • @vikashverma9
    @vikashverma9 4 роки тому +1

    how to calculate time complexity when we solve by bottom up dp approach.

    • @techdose4u
      @techdose4u  4 роки тому +1

      TIME will be same either you do Top down or Bottom up because you will be doing same number of operations using the same algo and the same logic path. If you are getting confused then just rotate the matrix by 180 degrees them bottom up approach will become top down.

    • @vikashverma9
      @vikashverma9 4 роки тому

      @@techdose4u Thanks for your quick reply. I am using recursive approach. Please see code.
      ide.geeksforgeeks.org/BYt1SFzNms

  • @prathameshmahankal4180
    @prathameshmahankal4180 Рік тому

    Could someone please help me understand how the time complexity for the last solution is O(N^2)?

  • @jatinshrivastava4231
    @jatinshrivastava4231 3 роки тому

    What should I do if i also have to count the number of paths through which We can reach the minimum cost as there can be more than one such paths?

  • @kumaraakash25
    @kumaraakash25 2 роки тому

    Amazing explanation but why would anyone go with the DP approach if complexity is the same in both recursion and DP both

  • @InsightWeaver
    @InsightWeaver 2 роки тому

    @tech dose what if add diagonal movement to it? how would we solve it then?

  • @aneeshmv8201
    @aneeshmv8201 4 роки тому +1

    Memoization of recursive code to remove overlapping sub problem calls, will give O(N*N ) right?

    • @techdose4u
      @techdose4u  4 роки тому

      It will depend on no of repeating subproblems. Actually 2^N is a much larger upper bound due to the constraint in movement. But yes, memoization should give approx N^2. But since we are still doing recursion, actual runtime can go much higher than table method if you get large matrix. So, table method is best for larger cases. I hope you got it :)

  • @kbhargavi4400
    @kbhargavi4400 4 роки тому +2

    TQ bro!😃

  • @yashgoswami5374
    @yashgoswami5374 4 роки тому

    What will be the solution if we allow to move in all 4 directions?

  • @sharvyahmed
    @sharvyahmed 4 роки тому +1

    👏👏👏👏👏👏

  • @RahulKumar-qu1if
    @RahulKumar-qu1if 4 роки тому +1

    Thank you!🔥

  • @alnaifmilon5420
    @alnaifmilon5420 4 роки тому +1

    Thank you