Would like to see your explanation of longest palindromic subsequence (516 on leetcode). Your explanation for checking if general input string is a palindrome was great
@KevinNaughtonJr
5 жыл бұрын
@@thekewlwaffle I'll see what I can do thanks for the suggestion!
@KevinNaughtonJr
5 жыл бұрын
@@rahulshrivastava3040 You got it!
@KevinNaughtonJr
5 жыл бұрын
@@rahulshrivastava3040 You got it!
@ophir1982
4 жыл бұрын
Great and simple solution. Once recommendation though is that when explaining such problem - demonstrate how the 2d array is filled visually, because it's hard to understand otherwise.
@midasama3124
4 жыл бұрын
Not only the solution was great, but most importantly I found the explanation on how to come up with it amazing. I really struggle with these dynamic programming problems and you really made it seem so easy!
@KevinNaughtonJr
4 жыл бұрын
Miguel Manchola if you help with these kinds of problems be sure to subscribe to The Daily Byte! thedailybyte.dev/
@raihanhaider5649
5 жыл бұрын
Thank you for the straight forward explanation.I'm in love with your channel
@KevinNaughtonJr
5 жыл бұрын
Anytime and I'm so happy to hear that share it with others I wanna help as many people as possible and thanks for your support!!!
@adampzakaria
2 жыл бұрын
Kevin your videos are outstanding, an absolutely tremendous resource. You’re personable and your explanations are excellent - so much easier to understand than anything else I’ve encountered.
@ghost_busters
2 жыл бұрын
Out of all other DP problems, THIS is the one which made me understand how easy DP solution can be .. Will apply for other similar problems, cool ...
@MorisonMs
5 жыл бұрын
There is a simple close form solution to this problem: (m-1+n-1) choose (m-1) Every path is composed of: m-1 moves down n-1 moves right m-1+n-1 moves total Choose a path is equivalent to determining when we choose to move down (m-1) out of all optional moves (m-1+n-1)
@VishalSharma-hq5ti
4 жыл бұрын
True, just to add it could be (m+n-2) Choose (n-1) also(same as down, we could also choose n-1 right moves).
@anujmodi1433
3 жыл бұрын
its a permutation-combination problem, basically the total being n!/a!/b! where a+b = n... in first example, a = 2 and b = 1 since the robot can move at max twice towards right and at max one downwards... total will always be 3 moves, which is 3!/2!/1! = 3... in second. similarly a = 6 and b = 2, n = 8 and 8!/6!/2! = 28 and so on... this isnt technically a programming challenge at all, and certainly not for Dynamic Programming...
@yueli2146
5 жыл бұрын
Finally getting some intuitions on dynamic programming! Thank you. First DP question I was able to do before watching your solution lol.
@jnsmelinda
3 жыл бұрын
There is that nice white wall behind you. You should buy some markers to match and draw on it. :)
@Amy-tw3zh
4 жыл бұрын
The usual approach for dynamic programming: 1. come up with a recursive algorithm (which is slow), 2. Come up with a dynamic algorithm (way waster) that bases the logic off of the recursive one. Build a 1-d or 2-d table as needed using loop/s and recursive algorithm logic BUT use indexing instead of recursive calls for example recCall(x-1,y-1) = recCall[x-1][y-1].
@Drake811
3 жыл бұрын
This is an older video but for future viewers it's worth pointing out that there is a more optimal way to solve this problem by treating it as a combinations problem instead of a programming problem. For every MxN graph you will always take (M - 1) downward movements and (N - 1) right movements. Every unique path is just a combination of these down and right movements. The math formula to calculate the number of unique combinations would be: (Total number of movements)! / ((Number of downward Movements)! * (Number of right movements)!) Total number of movements would be (M + N - 2) Number of downward movements would be (M - 1) Number of right movements would be (N - 1) The only part you would have to program is the factorial calculation and your space complexity would be O(1) and your runtime complexity would be O(M + N) due to the factorial calculations.
@nG27227
Жыл бұрын
Was wondering if anyone would point this out. No videos ever seem to mention the combinatorial solution for this prpblem, only DP. Sometimes it helps to know your combinatorics / discrete math :) Btw, I believe the binomial coefficient computation can be implemented more efficiently than O(m+n) -- that is, O(min(m,n))
@stssports2255
3 жыл бұрын
Am I just dumb or is there a reason people aren't solving this problem recursively? m = 3 n = 7 d = 0 r = 0 count = 0 def recurse(d,r): global count if d == m-1 and r == n-1: count += 1 if d < m-1: recurse(d+1,r) if r < n-1: recurse(d,r+1) recurse(d,r) print(count)
@Sophia-fw1rm
2 жыл бұрын
Can I see this is actually an exercise I did in my elementary school in a math contest lol. Thanks to that experience!
@samanthkrishnamurthy
2 жыл бұрын
I have watched many other videos but your explanations are so simple and easy to understand and they are alll 6-8 minutes others take 20-30 min and they confuse with lot of unnecessary details :-)
@philbowden
9 ай бұрын
Hi Kevin. Nice video. Very easy to follow. What about Big O? Would you says O(n2) (quadratic) for time complexity and the same for space?
@axbax93
4 жыл бұрын
Thank you Kevin!! iwanna be like you when i grow up... wait.. i'm a grown ass adult 😭
@surajgrandhi6742
4 жыл бұрын
Amazing approach brother please tell me how to get that approach I am looking at problem couldnt able to do it & simply looking at solution.
@navdeepmandal4338
4 жыл бұрын
this question belongs to a programming paradigm known as dynamic programming(dp), just give it a read on sites ike gfg and u will understand how to identify if it is a dp problem and how to get to this approach. best of luck.
@DataStructures
Жыл бұрын
wow that actually makes a lot of sense
@zacharysong4863
2 жыл бұрын
I would like to call you DP-Master.
@saulgoodman980
4 жыл бұрын
It's possible to bring down the space complexity to O(n), by overwriting on the same row again and again.
@xiaoyu88
5 жыл бұрын
hey you could just use 1D array(sizeof rows, init to 1s), as you only care about one row at a time, to calculate the next one. you can calculate each step with previous one (still going on loop with i and j) but just summing up dp[j] = dp[j] + dp[j-1] and then returning dp[n-1] so youll get it with O(n) space. hope this helps someone!
@BadriBlitz
3 жыл бұрын
Excellent explanation. You got a new subscriber.
@vk1618
4 жыл бұрын
Top-bottom approach of dymanic progamming. Which of dp problems could be solved without recursion?
@ridhwaans
3 жыл бұрын
is there a way you can memoize your solution to reach a more optimal solution?
@seunghunlee6778
5 жыл бұрын
Now it's clear! I can visualize it! Thanks Kevin🙏🙏
@KevinNaughtonJr
5 жыл бұрын
Super happy to hear that Seunghun and anytime!!! :)
@SameerSrinivas
4 жыл бұрын
Great video. It helps if you can bring visual description as well to the video.
@galanoth17
2 жыл бұрын
Can't we use dfs(right), dfs(down) recursive calls. Once you get to the end, then increment the num_paths counter?
@jocavuleta
5 жыл бұрын
Awesome and it does. True subscribers will know the satisfaction in those words... 😃
@KevinNaughtonJr
5 жыл бұрын
Thanks Jovan :)
@renukadolkar7288
5 жыл бұрын
Keep up the amazing work Kevin! Your videos are a blessing!
@renukasrishti4996
3 жыл бұрын
Thanks for the videos. They are really helpful. It will be great if you can discuss time and space complexity as well after each question.
@arturoislas1546
4 жыл бұрын
I could solve this by brute force, but it did not pass leetcode specs. Is there any way, any hint on how to identify DP problems? Maybe a video about it would be great!
@harshitgupta7902
4 жыл бұрын
sir,can u please teach us about recursion...because i am new to programming and these topics are difficult to understand..
@AidarMHTV
4 жыл бұрын
An alternative way of thinking could be treating array as a graph. Doing dfs (without marking ofc) and increasing counter whenever we reach final point will give result. Just might be easier for someone.
@ssbooth
4 жыл бұрын
I would love to know how you can calculate the unique paths if you were allowed to move both UP and LEFT as well (without overlapping any previously traversed squares). I'm working on a project that will need to incorporate this but so far the only examples I can find are for unique paths that calculate the shortest routes only, i.e. a matrix that looked like [[0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]] should, in theory, have 4 unique paths but most codes I've found only validate 3. I assume it's just more complex version of what you've done above?
@kamalhm-dev
5 жыл бұрын
This took me hours to understand but finally I got it!
@FrankLi92
5 жыл бұрын
I've done this problem before and had to stare at code for a long time to get it. You made it really straight forward. How would the solution be different if we could move in all 4 directions and we had an arbitrary start and end on a grid? Also I'd love if you could do the minimum window sliding problem. I get the concept, but the code sometimes is a bit hard to grasp. Thanks, Kevin!
@KevinNaughtonJr
5 жыл бұрын
Happy to hear my videos was helpful Frank, but don't stress about it it's a tough problem just keep working hard! My initial thoughts on what you're proposing is that...assuming you can't revisit cells (otherwise there would be infinite unique paths) I think it would be pretty similar except you would consider the other 2 directions as well when calculating the unique dp[i][j] and you wouldn't be returning the bottom right corner of the dp matrix anymore. Let me know your thoughts and thanks for the support Frank!
@KevinNaughtonJr
5 жыл бұрын
Also thanks for the problem suggestion I'll see what I can do :)
@vivekmishra1487
4 жыл бұрын
Hi Kevin, why [1][0], [2][0] ......& [0][1], [0][2]....are filled with 1. If [1][0] has 1, then [2][0] should have 2 because to come down we have to travel all cell of the column [0]. Sorry if I am wrong.
@algorithmimplementer415
5 жыл бұрын
You could also show us how to print those unique paths, as usually that is the follow up question in the interview.
@kunal_chand
4 жыл бұрын
I am thinking of using backtracking to print every unique path. What are your thoughts?
@prajmus
5 жыл бұрын
Audio level's much better :) great video!
@KevinNaughtonJr
5 жыл бұрын
Woooo so happy to hear that! Thanks for letting me know Kuba :)
@sanatanshrivastava3777
4 жыл бұрын
Is there anyway we can do this with like dfs or something?
@shilpaas2433
4 жыл бұрын
I thought it was a DFS problem
@sakshisengar7709
4 жыл бұрын
You made this so easy for me thanks a lot man.
@utkarshhhhhhh
2 жыл бұрын
What's the Time & Space Complexity ?
@galanoth17
2 жыл бұрын
This was my solution without using DP. Using recursion instead. class Solution { int paths = 0; public int uniquePaths(int m, int n) { findEnd(m,n, 0, 0); return paths; } void findEnd(int m, int n, int cur_row, int cur_col){ if (cur_row == m-1 && cur_col == n-1){ paths++; return; } if(cur_row < m-1){ findEnd(m,n, cur_row+1, cur_col); } if(cur_col < n-1){ findEnd(m,n, cur_row, cur_col+1); } } } Basically treating the grid as a graph and using depth first search till you get to the end. Once you get to the end, then you increment the paths counter. However, leetcode said time limit exceeded on this on an input of 23,12 When I run the function with that input, i get the answer in like 1 second on my computer. So i guess they are very particular about the time efficiency for this one
@umabharati9911
4 жыл бұрын
Hey Kevin, instead of writing two for loops for all the 1s, we can directly assign them in the main loop(inside 2 for loops) like this: int[][] res = new int[m][n]; for(int i=0;i0) res[i][j] = res[i-1][j]+res[i][j-1]; } } } return res[m-1][n-1]; Also, I wrote another solution using DFS, can you tell me why is LC showing time limit exceeded for this? It gives the same output as your code if I run in eclipse. private static int unique(int m, int n) { int[][] matrix = new int[m][n]; return findpaths(0,0,matrix,m,n) ; } static int findpaths(int i,int j,int[][] matrix,int row, int col){ if(i=row || j=col) return 0; if(i==row-1 && j == col-1) return 1; return findpaths(i+1,j,matrix,row,col)+findpaths(i,j+1,matrix,row,col); } Please help :(
@pratheek5096
4 жыл бұрын
This was the first DP question I solved Thanks for this approach
@KevinNaughtonJr
4 жыл бұрын
Anytime! If you like my explanations be sure to check out the interviewing service I created thedailybyte.dev/?ref=kevin I'd recommend joining the annual plan if you can!
@PrasannDeshpande
3 жыл бұрын
I've done this problem before. But today I feel like I can do this problem even after decades if asked
@darod6098
4 жыл бұрын
Wow. Pretty difficult concepto explained tooooooo easily and clear. Thank you
@darod6098
4 жыл бұрын
btw the problem link in the description is incorrect. it is this one leetcode.com/problems/unique-paths/
@anindyapaul113
4 жыл бұрын
Awesome solution.. Thanks bro..
@mallikakejriwal3729
4 жыл бұрын
I was doing via dfs, it worked for some inputs and after that gave me timeout. Still wondering why? as DFS is also O(N)
@divyanirao4279
4 жыл бұрын
The dfs solution is actually O(2^n) since each recursive call makes 2 more calls such as uniquePaths(m-1, n) + uniquePaths(m, n-1). It times out since this takes exponential time
@Niro_Hero
3 жыл бұрын
Kevin, you are so awesome!
@hamzaaitboutou8563
5 жыл бұрын
Can't this just be solved in O(1) with combinatorics ?
@ArkabandhuChowdhury
4 жыл бұрын
The formula is (m+n)!/(m!*n!). But it's still O(n).
@arungopalan
4 жыл бұрын
No. Even if combinatorics, calculating the actual value is O(n).
@VishalSharma-hq5ti
4 жыл бұрын
@@ArkabandhuChowdhury formula is (m-1+n-1)!/ (m-1)!(n-1)!
@sujeeshsvalath
4 жыл бұрын
Man u made it look so easy. Thank you legend
@jigyasuprakash911
4 жыл бұрын
The made DP seems very simple.. Thank you so much sir🙏🙏
@KevinNaughtonJr
4 жыл бұрын
Jigyasu Prakash anytime! If you need help with these kinds of problems sign up for the interviewing service I created thedailybyte.dev/?ref=kevin I recommend joining the annual tier if you can!
@priyankabagul494
5 жыл бұрын
Your are awesome, only one request just boost volume its bit low
@abhishekverma5928
4 жыл бұрын
Hi, Kevin. Thanks for your channel. I really like your video very much. Kevin Can you please help me in understanding this. Here in example- diagram they show the 2D- Array for 7 * 3 as Rows=3 and column=7. After that in their first example where m=3 and n=2. Again they are taking Rows=2 and column=3 represented by int[][] dp = new int[n][m]. Thats why they get answer like 1. Right -> Right -> Down 2. Right -> Down -> Right 3. Down -> Right -> Right 1 -> 1 -> 1 1 -> 1 -> 1 R R 1 -> 1 -> 1 D | D| | D 1 -> 1 -> 1 R R But in you code you take m = row and n = column and create int[][] dp = new int[n][m]. And thus the 2D Array I got is . 1 1 1 1 1 1 I know , I wrote very much, But may be I got confused. Please help me out in getting this.
@abhishekverma5928
4 жыл бұрын
I think this happens because either we took int[] dp = new int[3][2] or we took int[] dp = new int[2][3], we got the same answer. we can use row and column interchangeably.
@equaco
5 жыл бұрын
Put the code up on github pls.Also if you do the beginning explanation with some graphics or powerpoint I think your videos will improve a lot.
@KevinNaughtonJr
5 жыл бұрын
Thanks for the advice Equaco! I'd always thought about putting diagrams or graphics on the videos maybe I'll start doing it!
@lifeofme3172
4 жыл бұрын
I didn't understand why we return dp[n-1][m-1] , other parts were clear.
@freddycolon8789
4 жыл бұрын
Because since its dynamic programming, you are looping through each cell and finding how many ways there are to get to that cell by calculating the different ways from the cells it could of came came from (top or left). Meaning you are entering the different ways to get to that cell you are on into the cell so the last cell of the matrix, which would be matrix[m-1][n1], will have the answer to all the possible ways to get to the last cell in the matrix. top left corner to bottom right. if you just put matrix[m][n] youll get an out of bounds. since m is the number of rows if there are 4 rows but the first row is matrix[0] the last row would be row 3 aka matrix[m-1]
@MridulManocha
4 жыл бұрын
Can you make a video on Unique Paths III please?
@KevinNaughtonJr
4 жыл бұрын
I'll see what I can do. In the meantime if you need help with these kinds of questions be sure to join the interviewing service I created: thedailybyte.dev/?ref=kevin I'd recommend joining the annual plan if you can!
@stonecoldcold2941
4 жыл бұрын
nice one mate
@shrimatkapoor2200
4 жыл бұрын
I can't believe that I didn't realise that this was a DP question in a grid, damn it!!
@anujmodi1433
3 жыл бұрын
it isnt. its a permutation-combination problem, basically the total being n!/a!/b! where a+b = n... in first example, a = 2 and b = 1 since the robot can move at max twice towards right and at max one downwards... total will always be 3 moves, which is 3!/2!/1! = 3... in second. similarly a = 6 and b = 2, n = 8 and 8!/6!/2! = 28 and so on... this isnt technically a programming challenge at all, and certainly not for Dynamic Programming...
@shivangishukla2629
4 жыл бұрын
Please make a video on how to match a string and a pattern without using
@mahmoudnasr8710
5 жыл бұрын
you are brilliant sir!!!! please keep on going
@surajgrandhi6742
4 жыл бұрын
How the time will be 0 whether it takes O(n^2) time complexity??
@navdeepmandal4338
4 жыл бұрын
bcos, though in dp it seems like we have o(n^2) time complexity but actually we only fill up a 2d array and that takes really no time to compute and fill it, and time shown as zero is compilation time, if u will practice some more dp problems, u will find some more zeroes, so best of luck!
@mattbialek9633
4 жыл бұрын
amazing videos
@KP-jx1wy
5 жыл бұрын
I swear whoever first thought of this is a mutant.
@AtoZ-jv1py
4 жыл бұрын
thanks for your videos (great work, nice explaination);
@ankeetmaini
5 жыл бұрын
Can you do reconstruct itinerary in leetcode using dfs and backtracking? Thanks!
@KevinNaughtonJr
5 жыл бұрын
I'll see what I can do thanks for the suggestion!
@annajimmy2610
5 жыл бұрын
Thanks Kevin!It is an amazing strategy to solve this problem. I tried using combinations in python, but failed. Thanks a lot
@KevinNaughtonJr
5 жыл бұрын
Thanks Anna and don't worry about it it's a tough problem just keep working at it!!!
@shalinsitwala
4 жыл бұрын
Love your videos. You literally make every question look easy.
@KevinNaughtonJr
4 жыл бұрын
Thanks Shalin really appreciate that!
@vibekdutta6539
5 жыл бұрын
Happy Teachers Day Sir, appreciate your hard work and teaching!
@shwetadk1986
5 жыл бұрын
Thats was so easy !! You are so clear in your explainations. Thanks :)
@KevinNaughtonJr
5 жыл бұрын
Thanks for the kind works Shwetz and anytime! :)
@nishatsayyed8326
5 жыл бұрын
I am loving this channel ♥️
@navdeepmandal4338
4 жыл бұрын
This really helped sir, thanks a lot!
@21void5
4 жыл бұрын
i have learn a lot from you. thank you so much.
@djfedor
5 жыл бұрын
Kevin, do you remember any employer (except Google, Facebook possibly) asking to solve a DP problem on a real whiteboard/phone interview? Based on my experience, I don't remember such situation..
@KevinNaughtonJr
5 жыл бұрын
Aside from the companies you mentioned I can't say I've been asked DP problems explicitly, but you never know so I think it's best to prepare for them. Furthermore, chances are if you can do the dp problems you can do any other problems so it's always good to study the hard stuff :)
@elle4556
4 жыл бұрын
OMG How do you explain so well! I m still dumbstruck!
@KevinNaughtonJr
4 жыл бұрын
Thanks Anshika! If you like my explanations be sure to join the interviewing service I created to help people learn the most popular interview questions and topics! thedailybyte.dev/?ref=kevin I recommend joining the annual tier if you can! :)
@elle4556
4 жыл бұрын
@@KevinNaughtonJr Will definitely give it a try!
@tarankaranth8782
3 жыл бұрын
hard to visualize. explanation with pic would have helped
@rahulratra6672
4 жыл бұрын
Sir thanks a lot . You are doing a great job.
@KevinNaughtonJr
4 жыл бұрын
Thanks Rahul!
@ibrahimshaikh3642
4 жыл бұрын
Plz use paper or paint to explain
@dumitruchicu5907
2 жыл бұрын
The key question to the sub-problem definition & as well as the solution is @ #4:37
@nareshbabu9235
5 жыл бұрын
Hi Kevin Could you please upload a solution video on LeetCode 711 - Number of Distinct Islands II Problem ?
@KevinNaughtonJr
5 жыл бұрын
I'll see what I can do, thanks for the suggestion Naresh!
@Mal-wk3uq
5 жыл бұрын
How did you become good at solving problems ?this what I struggle with
@KevinNaughtonJr
5 жыл бұрын
Don't beat yourself up, these questions are hard!!! It took me a lot of practice and a lot of hours of studying to do these kinds of problems so don't worry just keep working hard!
@Mal-wk3uq
5 жыл бұрын
Kevin Naughton Jr. thanks man keep making these videos they do help.Do you have a book or any resource you’d recommend?
@KevinNaughtonJr
5 жыл бұрын
@@Mal-wk3uq Anytime and don't worry I will! I would recommend cracking the coding interview and the elements of programming interviews!
@kenwang5113
5 жыл бұрын
Thanks. rewrite it in python def findpath(m,n): nums=[[0 for i in range(n)]for j in range (m)] for i in range(m): nums[i][0]=1 for j in range(0,n): nums[0][j]=1 for i in range(1,m): for j in range(1,n): nums[i][j]=nums[i-1][j]+nums[i][j-1] print("Array Value ",nums) print("The max value is",nums[m-1][n-1]) findpath(17,3)
@mahmoudnasr8710
5 жыл бұрын
i have a question why do u prefer solving problems using java not C++ is there a reason or you just java i used to solve problems using C++ but once i saw ur videos am considering java what do u think ?
@saifullahtappalmuhammad4483
5 жыл бұрын
Jump Game (LeetCode). Please solve this question, using DP. Thanks.
@KevinNaughtonJr
5 жыл бұрын
I'll see what I can do thanks for the suggestion!
@aronblanche
3 ай бұрын
Thanks
@poonamyadav-qz7yt
4 жыл бұрын
why are you returning dp[m-1][n-1]? Why not dp[m][n] ?
@mattbettinson4576
4 жыл бұрын
Actual link: leetcode.com/problems/unique-paths/
@mr.mystiks9968
4 жыл бұрын
Not too sure what the purpose of this video is aside from seeing you code. You just blurt out how to calculate the unique paths for any cell, which is fine but you don’t explain any thought process for us to follow that leads you to figuring out that DP function. It’s like saying “Here’s the answer, you know why it’s the answer? Look it works so that why”. Also you can optimize the space usage here to be O(Min(n,m)) so this is only optimal for time.
@KevinNaughtonJr
4 жыл бұрын
Mr. Mystiks you’re welcome to leave the optimized space solution here
Пікірлер: 147