Minimum Path Sum

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

КОМЕНТАРІ • 96

  • @KevinNaughtonJr
    @KevinNaughtonJr  5 років тому +51

    Dynamic programming questions can be tricky so I hope this explanation made sense!

    • @ashutosh.sharma
      @ashutosh.sharma 5 років тому

      Thanks for the great explanation. please make a video on leetcode.com/problems/longest-univalue-path/ sometime in future. Thank you :)

    • @chiragchatwani9124
      @chiragchatwani9124 5 років тому

      Hey You make awesome content but can you make a video dedicated on How to use collection framework and all its method I am new to java and its very confusing please. Thanks in advance

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

      Line 10 could be : dp[i][j] = grid[i][j];
      to keep it simple.

  • @visheshaggarwal3395
    @visheshaggarwal3395 5 років тому +9

    at line 10: dp[i][j]+=grid[i][j] should be dp[i][j]=grid[i][j]; Result will be same in either case as their is nothing in dp[i][j] at start.

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

      I was confused on why he used +=, thanks for the clarification

  • @akashrajpurohit97
    @akashrajpurohit97 5 років тому +6

    Hey Kevin.. Thanks for your amazing videos.. The explanation is very clear and I am just about to finish you 88+ long Playlist of LeetCode questions and in every question I have learned something new which have helped to crack the first interview round of a company that I attented couple of weeks back.. The second round is tomorrow and I'm preparing myself by going over your Playlist. Just wanted to say thanks for doing this.. Appreciate it.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +3

      Akash Rajpurohit that's awesome Akash and anytime I'm happy to hear my vids have been helpful! Good luck with the interview tomorrow and let me know how it goes!!!

    • @akashrajpurohit97
      @akashrajpurohit97 5 років тому +2

      @@KevinNaughtonJr It went awesome.. Was asked one Algorithmic question which I was able to solve in optimized way in around 20 mins.. Rest 45-50 minutes there was discussion and questions based on my resume and projects. The silver lining to it was in the end the interviewer told that he was impressed by my work on side projects and other freelance projects that I do. Will hear from them in about a week's time about the results. Hoping for positive feedback. Solving problems along with you by following your thought process on how to tackle certain types of question truly helped me. You're awesome.. Thanks again✌️

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

      @@akashrajpurohit97 Hey! Curious to hear your journey p:

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

    @Kevin , Thanks for the video. How about if the question is finding the actual paths that lead to the minimum sum? So instead of returning the minimum sum value, we should return all those paths (vector). This is an interview question and has been puzzling me.

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

    I saw many of your videos.
    Best thing about your solutions are they are very very intuitive.
    Once you spend 5-10 min on the video, you'll never forget it.
    Keep making more videos of complex problems.
    thanks

  • @meenameiss
    @meenameiss 5 років тому +29

    Definitely went and did it before i watched the video.
    I did it in space(1) by just reusing the given matrix instead of creating an auxiliary one. (I'm sure you thought of that but wanted to keep it simpler)
    Thank you for the content, keep going! Maybe next time finish with a follow up with potential improvements even if you don't code them

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +5

      Pedro Silva anytime and good stuff that's an awesome optimization!!! And good idea I'll see what I can do about mentioning potential improvements during vids :)

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

    Thank you so much for this! This came up in 30day leetcode challenge and I was having trouble as I was trying to implement priority queue but dp made it so much simpler

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

      Yo bro, I also came here because of the Leetcode challenge. Although I implemented it using DFS and I was getting TLE for larger inputs

  • @FernandoTakeshiSato
    @FernandoTakeshiSato 5 років тому +1

    I went ahead and attempted a graph search, only to have the tests blow up with time limit exceeded - I even added some optimizations, but no cigar. In this particular case, it seems like DP is the way to go. Anyway, one potential optimization (aside from reusing the provided grid), is iterating through the first row and first columns in their entirety first, so when you go into the double fors, you start fom 1 instead of 0 and there's no need to check the indexes. This way you eliminate the if statements, at the price of splitting up the code a bit. I think runtime will be the same, since you're not double checking / calculating anything.
    Anyways, thanks for the videos man. I've got on site interviews with Amazon coming up and am running through the videos you marked as questions they asked, so I can kind of focus my studies a bit. Cheers!

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

    Thanks!

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

    Can you please make a list of questions asked in Amazon of leet code premium . Because some of the students are not comes such background that buy a premium account. Looking positive response from your side.
    Thank you.

  • @sateeshtata
    @sateeshtata 5 років тому +3

    Nice explanation. Thank you for discussing the SC and TC at the end :)

  • @100bands
    @100bands 4 роки тому

    I got confused when I saw the inner loop going from 0 - dp[i].length
    It works fine with dp[0].length and it's less confusing. The arrays will all have the same length anyway.
    So this is fine:
    for(int i = 0; i < dp.length; i++){
    for(int j = 0; j < dp[0].length; j++){
    .........
    }
    }

  • @vernon_4411
    @vernon_4411 5 років тому

    For clarification, the code between line 10 and line 17 can be rerrangened by using assigment insteadl of adding, dp[i][j] = ... + grid[i][j];

  • @amaleldick9899
    @amaleldick9899 5 років тому +12

    Hey Kevin! Really love your videos - I've been struggling with understanding Longest Palindromic Substring for a while now, could you solve it sometime?

  • @jlecampana
    @jlecampana 5 років тому +4

    AWESOOOOME!!!!! I love the DP Videos! Thank you Kevin Naughton Jr.

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

    Rips off shirt and dances when Kevin solves problem

  • @umabharati9911
    @umabharati9911 5 років тому +1

    Hi Kevin, That's a good explanation. Is it possible to do it in the DFS method? Would you please tell me where am I going wrong in the below code?
    public static int minPathSum(int[][] grid) {
    int row = grid.length;
    int col = grid[0].length;
    return findMinPath(row,col,0,0,grid,0);
    }
    public static int findMinPath(int row, int col,int i, int j,int[][] grid,int sum){
    if(i= row || j=col ) return 0;
    sum +=grid[i][j];
    if((i == row-1 && j==col-1)) return sum;
    int x = findMinPath(row,col,i+1,j,grid,sum);
    int y = findMinPath(row,col,i,j+1,grid,sum);
    return Math.min(x,y);
    }
    I am waiting for your response. Please help me out. Thank you!

  • @goodwish1543
    @goodwish1543 5 років тому +2

    Straight to the point. Good job.

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

    Hi Kevin, thanks for your effort. Really helpful.
    One request though, could you please spend some time on your thought process while coming to the optimal solution/algorithm from brute force approach? this will really help us.
    Like for this problem, my first thought was backtracking, as at each point, we have two choices, either go right or down. if the chosen one does not lead to min cost then backtrack and select the other choice and see if it yields min cost.

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

      Honestly the thought process for algo questions is something that will come with time the more experience you have, the more questions you do that are similar to this one youll build a habit of doing it a specific way.

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

    Thank you for the explanation. Very clear instruction and implementation.

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

    Could you please please make a playlist for DP ? I'm struggling with DP and would love it if you could solve many more DP problems starting from easy to hard problems

  • @chiragchatwani9124
    @chiragchatwani9124 5 років тому +3

    Hey You make awesome content but can you make a video dedicated on How to use collection framework and all its method I am new to java and its very confusing please. Thanks in advance

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

    Dude, I love this solution! It seems so intuitive now, wow!

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

    Can you suggest one good book on data structures and algorithms? I have searched on internet and find like so many books and it's really confusing. If you can suggest one book which is going to cover everything and can be used as a good reference for preparing for coding interviews then that will be great. And if that book is written for c++, then that's even better.

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

    We can use the same matrix passed in input param as our DP matrix, no need of extra memory.

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

    Why does this need DP? Can it not be done using BFS with min-heap to give shortest next index? Is it not similar to Dijkstra's shortest path algorithm?

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

    What is the use of += at line no 10. there will not be any value in dp[i][j] to add with.

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

    Thanks Kevin, quite simple and elegant solution :)

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

    Thank you so much! Literally the best explanations ever for leetcode :D

  • @AtoZ-jv1py
    @AtoZ-jv1py 4 роки тому +1

    i was struggling from 10 days & u did in 8 min. only (nice)

  • @sojoru
    @sojoru 5 років тому +5

    I don't know how well you will take my comment but I just want to say that I tried to watch this video over again just to understand the process but I couldn't even get one thing. I get how to solve it but nothing else. I came to this video so I could have some visualization but you just went through everything without even pointing to or giving any visualization of what you were talking about. And the only visualization I receive was you typing the code which wasn't that helpful. You don't have to change anything just saying what I thought. Thanks again for the video.

  • @faraz7409
    @faraz7409 5 років тому +2

    Another great Vid - Thanks Kevin!!!

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

    I don't understand the question. How can I understand the question? What questions should I ask?

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

    Great Explanation, Keep it up Buddy🤑

  • @BookWormsOriginal
    @BookWormsOriginal 5 років тому

    Nice explanation, keep on posting new questions!

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

    Thank you Kevin, nice explanation :)

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

    Hi Kevin, can I have solution in Php please ?

  • @tianyangren
    @tianyangren 5 років тому

    My friend gets asked this question, but it needs to output the actual path. Any thoughts?

  • @rakshith3547
    @rakshith3547 5 років тому

    Hey Kev.. you are awesome bro..but 1 suggestion would be to trace the problem by taking an example which would be more effective to understand.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому

      Thanks for the suggestion Rakshit, I'll see if I can do that in future vids!

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

    really awesome one thank you!

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

    memorization makes world easy 🌀🌀🌀😍

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

    sir u made it so easy to understand

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

    Really good explanation!

  • @Krishna42570
    @Krishna42570 5 років тому +14

    Pls increase rate of uploading videos per dayyyy.....

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +1

      Jonnalagadda Gopi krishna haha I'll try to churn out vids more often but no promises yet :)

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

    Why not add the values in place?

  • @chiragchatwani9124
    @chiragchatwani9124 5 років тому +3

    WOW MAN GREAT STUFF

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +1

      Chirag Chatwani THANKS CHIRAG!!!

    • @chiragchatwani9124
      @chiragchatwani9124 5 років тому

      @@KevinNaughtonJr Hey You make awesome content but can you make a video dedicated on How to use collection framework and all its method I am new to java and its very confusing please. Thanks in advance

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

    what about BFS?

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

    Hot to sell t shirt which you have added?

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

    bless your solution. godsend

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

    Let's goooo now I understand dp!

  • @Dan-tg3gs
    @Dan-tg3gs 4 роки тому

    you could've done that problem without making a copy of the matrix.

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

    Great explanation

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

    I used the recursive approach, getting WA for some test cases, can anyone spot my mistake:
    class Solution {
    public int minPathSum(int[][] grid) {
    int row=grid.length-1;
    int col =grid[0].length-1;
    int sum = grid[row][col];
    int path =findpath(row,col,sum,grid);
    return path;
    }
    public int findpath(int row,int col,int sum,int[][] grid){
    if(row

  • @kamalhm-dev
    @kamalhm-dev 5 років тому +1

    Questions like these really need some visual explanation

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +1

      Kamal Mahmud yeah that's a good idea maybe I can try and add some visuals

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

    you r simply awesome my friend

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

    Thanks, Kevin!

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

    Thanks!

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

    Thank you

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

    Thanks a lot man!

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

    Am I the only one who did not understood the solution

  • @jamesnguyen5810
    @jamesnguyen5810 5 років тому +1

    thanks dude

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

    I came here to seee how to retrieve dat path from where i got the ans🙁
    Anyways Ur explanation is gud

  • @MK-xl5yo
    @MK-xl5yo 5 років тому +1

    im sure before video you solved it many times or watched in discussion panel for possible solution.. way better to see process how you came to this solution, not straight forward solution you just memorized.

  • @bee.g.s.ashuwin215
    @bee.g.s.ashuwin215 3 роки тому

    Awesome explanation!