Minimum Path Sum

  Рет қаралды 57,114

Kevin Naughton Jr.

Kevin Naughton Jr.

Күн бұрын

Пікірлер: 96
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
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.
@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 жыл бұрын
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 жыл бұрын
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 :)
@akashrajpurohit97
@akashrajpurohit97 5 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
@@akashrajpurohit97 Hey! Curious to hear your journey p:
@visheshaggarwal3395
@visheshaggarwal3395 5 жыл бұрын
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 3 жыл бұрын
I was confused on why he used +=, thanks for the clarification
@WarrenMoofett
@WarrenMoofett 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 жыл бұрын
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!
@darshantsdarshan1
@darshantsdarshan1 4 жыл бұрын
Fernando Takeshi Sato great efforts!
@sateeshtata
@sateeshtata 5 жыл бұрын
Nice explanation. Thank you for discussing the SC and TC at the end :)
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Sateesh Tata anytime and thanks Sateesh!
@ChocolateMilkCultLeader
@ChocolateMilkCultLeader 4 жыл бұрын
Rips off shirt and dances when Kevin solves problem
@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++){ ......... } }
@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.
@amaleldick9899
@amaleldick9899 5 жыл бұрын
Hey Kevin! Really love your videos - I've been struggling with understanding Longest Palindromic Substring for a while now, could you solve it sometime?
@nash_tech
@nash_tech 4 жыл бұрын
Thank you for the explanation. Very clear instruction and implementation.
@goodwish1543
@goodwish1543 5 жыл бұрын
Straight to the point. Good job.
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Good Wish thanks!
@sanyamgoel1259
@sanyamgoel1259 3 жыл бұрын
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.
@jlecampana
@jlecampana 5 жыл бұрын
AWESOOOOME!!!!! I love the DP Videos! Thank you Kevin Naughton Jr.
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
jlstr anytime!!! Glad you enjoyed :)
@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];
@OverLordOfDa3rdWorld
@OverLordOfDa3rdWorld 4 жыл бұрын
Dude, I love this solution! It seems so intuitive now, wow!
@KevinNaughtonJr
@KevinNaughtonJr 4 жыл бұрын
thanks!
@umabharati9911
@umabharati9911 5 жыл бұрын
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!
@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
@anushree3744
@anushree3744 4 жыл бұрын
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 жыл бұрын
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.
@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
@dipukrishnan6383
@dipukrishnan6383 3 жыл бұрын
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.
@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?
@tonyconor681
@tonyconor681 2 жыл бұрын
memorization makes world easy 🌀🌀🌀😍
@theunknownguy354
@theunknownguy354 2 жыл бұрын
really awesome one thank you!
@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
@BookWormsOriginal
@BookWormsOriginal 5 жыл бұрын
Nice explanation, keep on posting new questions!
@santhiyanaga
@santhiyanaga 11 ай бұрын
Thanks!
@KevinNaughtonJr
@KevinNaughtonJr 11 ай бұрын
thank YOU i really appreciate it! :)
@navneetrabadiya7776
@navneetrabadiya7776 4 жыл бұрын
We can use the same matrix passed in input param as our DP matrix, no need of extra memory.
@sojoru
@sojoru 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.
@AtoZ-jv1py
@AtoZ-jv1py 4 жыл бұрын
i was struggling from 10 days & u did in 8 min. only (nice)
@faraz7409
@faraz7409 5 жыл бұрын
Another great Vid - Thanks Kevin!!!
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Faraz thanks Faraz I hope you enjoyed it!
@dipakraut6058
@dipakraut6058 4 жыл бұрын
Great Explanation, Keep it up Buddy🤑
@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.
@sureshgarine
@sureshgarine 3 жыл бұрын
Thank you Kevin, nice explanation :)
@channeltastypizza
@channeltastypizza 3 жыл бұрын
bless your solution. godsend
@supremoluminary
@supremoluminary 4 жыл бұрын
I don't understand the question. How can I understand the question? What questions should I ask?
@AolaDIY
@AolaDIY 4 жыл бұрын
Really good explanation!
@KevinNaughtonJr
@KevinNaughtonJr 4 жыл бұрын
thanks!!!!
@tianyangren
@tianyangren 5 жыл бұрын
My friend gets asked this question, but it needs to output the actual path. Any thoughts?
@krupapatel4623
@krupapatel4623 2 жыл бұрын
Hi Kevin, can I have solution in Php please ?
@Krishna42570
@Krishna42570 5 жыл бұрын
Pls increase rate of uploading videos per dayyyy.....
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Jonnalagadda Gopi krishna haha I'll try to churn out vids more often but no promises yet :)
@praphulyadav5065
@praphulyadav5065 4 жыл бұрын
sir u made it so easy to understand
@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 жыл бұрын
Questions like these really need some visual explanation
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Kamal Mahmud yeah that's a good idea maybe I can try and add some visuals
@martialcoder
@martialcoder 3 жыл бұрын
Hot to sell t shirt which you have added?
@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!
@codearabawy
@codearabawy 4 жыл бұрын
Thanks, Kevin!
@akshatdhiman8819
@akshatdhiman8819 4 жыл бұрын
you r simply awesome my friend
@Dan-tg3gs
@Dan-tg3gs 4 жыл бұрын
you could've done that problem without making a copy of the matrix.
@chiragchatwani9124
@chiragchatwani9124 5 жыл бұрын
WOW MAN GREAT STUFF
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
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
@shivamshah431
@shivamshah431 4 жыл бұрын
Why not add the values in place?
@farukarshad
@farukarshad 3 жыл бұрын
what about BFS?
@amateursoundz6262
@amateursoundz6262 2 жыл бұрын
Let's goooo now I understand dp!
@alvxlopez
@alvxlopez 4 жыл бұрын
Thank you
@rajenshrestha8795
@rajenshrestha8795 2 жыл бұрын
Thanks!
@AmarjotBhullar
@AmarjotBhullar 4 жыл бұрын
Thanks a lot man!
@jamesnguyen5810
@jamesnguyen5810 4 жыл бұрын
thanks dude
@KevinNaughtonJr
@KevinNaughtonJr 4 жыл бұрын
Anytime dude
@shivamtyagi9936
@shivamtyagi9936 4 жыл бұрын
I came here to seee how to retrieve dat path from where i got the ans🙁 Anyways Ur explanation is gud
@nizarkadri3109
@nizarkadri3109 3 жыл бұрын
Am I the only one who did not understood the solution
@MK-xl5yo
@MK-xl5yo 5 жыл бұрын
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!
Partition Equal Subset Sum
16:51
Kevin Naughton Jr.
Рет қаралды 78 М.
Reorganize String
12:44
Kevin Naughton Jr.
Рет қаралды 78 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 26 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 35 МЛН
Deadpool family by Tsuriki Show
00:12
Tsuriki Show
Рет қаралды 7 МЛН
Range Sum of BST
8:39
Kevin Naughton Jr.
Рет қаралды 30 М.
15 Years Writing C++ - Advice for new programmers
4:04
SyncMain
Рет қаралды 1,3 МЛН
Path Sum II
8:35
Kevin Naughton Jr.
Рет қаралды 42 М.
Binary Tree Paths
7:53
Kevin Naughton Jr.
Рет қаралды 42 М.
How to become a cracked dev
12:31
Coding Jesus
Рет қаралды 8 М.
Spiral Matrix
10:16
Kevin Naughton Jr.
Рет қаралды 45 М.
Binary Tree Right Side View
8:28
Kevin Naughton Jr.
Рет қаралды 41 М.
Israel Has The Right To Defend Itself | Stand-up Comedy by Daniel Fernandes
15:07
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 26 МЛН