Unique Paths | Dynamic programming | Leetcode #62
Vložit
- čas přidán 24. 07. 2024
- This video explains an important dynamic programming interview problem which is to count all possible unique paths to reach from first cell to the last cell in a grid.We can solve this problem simply by using recursion but it is a costly process in terms of time.We can improve recursive solution by using a
lookup table which is called memoization.The most optimal approach is to solve using tabulation dynamic programming approach.I have shown the TOP-DOWN dynamic programming approach.I have explained the entire problem step by step by using proper examples and intuition for each step.I have dry run the algorithm and have also explained the code walk through at the end of the video.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
=================================================================
INSTAGRAM: / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
=================================================================
CODE LINK: gist.github.com/SuryaPratapK/...
OTHER PROBLEMS:-
Dungeon Game: • Dungeon Game | Dynamic...
Coin change problem: • Coin Change 2 | Dynami...
Largest divisible subset: • Largest Divisible Subs...
Somebody give this man a medal ASAP. Crystal clear explanations on each and every problem. Very Inspiring and Helpful.
🤣 Hahahahaa.....Thanks for appreciating :D
I like Your Dark theme in the videos which gives my eyes the power to view more.
This man is so good at segementing the problems ! More power to you bro! A Die hard fan of you bro 💗
Thanks :)
I think TechDose Sir has joined Google. Congratulations.
Can't thank you enough, I was stuck for hours with this approach. Now I understand it completely.
I think TechDose Sir has joined Google. Congratulations.
Everyone starts with matrix directly no one says why this would work. this Man is someone who tells the exact logic behind it. Great work Mate ...
Thanks :)
You are way underrated for your work. Thanks for the solutions!
Welcome :)
Most underrated channel ! Hatsoff to you brother !
Thanks :)
Mind-blowing Explanation
I am a fan of the way you think and solve the problem bro
I think TechDose Sir has joined Google. Congratulations.
This universe needs people like him!! Protect him :)
Thank you so so much
You have such a great talent ❤️ of explaining complex things like piece of cake.
Welcome :)
Start watching at 10:40. Great stuff btw, thanks for this.
Best explanation for the unique paths problem.
I have already solved this problem 2 months back, hence I copied my code from there. It was bottom up approach. Good to know about the top down approach.
I had shown bottom up in dungeon game and so I took top down this time XD
Top-down DP runs slightly faster (for some reason)
Simple solution and awesome explanation ! Too helpful.
Its been 10 months and this Guy still hasn't received a medal!!!! You deserve it man! Thanks for explaining this nicely.
Welcome :)
The best explanation,you deserve 1 Million subscriber .
Thanks ❤️
I think TechDose Sir has joined Google. Congratulations.
Very precise, clear, crisp explanation.
Really you make this problem very easy to understand 😊😇.. thanking you sir 😊
Welcome :)
Ur channel is a goldmine
What a superb explanations !! Really helped a lot.
Thanks
Sucessfully completed. Thanks for this workout
Welcome :)
One of the easiest DP problems 😅
Also the Robot in thumbnail is 😍
The Explanation is soo great....just came through an one line python solution.
return math.factorial(A+B-2)//(math.factorial(A-1)*math.factorial(B-1))
Thanks
Amazing explanation, thanks
very helpful to me! thanks alot..You deserve many more likes
Thanks
Iam grateful to your service sir
This is how a coding problem should be explained.............. Everybody needs to learn from this man right here.!!!!!! Hats off🎉🎉
Great explaination
Salute to ANYONE who gets this interview question and solves it within half n hr
I think TechDose Sir has joined Google. Congratulations.
Awesome, Sir what will be the complexity of we use combinatorics for the solution, I think there is a direct solution using combinatorics
By combinatorics its like
(m+n-2)C(n-1). An better SC approach though!!
Roger Knight how you explain this approach?
@@tecocode6070 this problem is eqivalent to permute (m-1) down move and (n-1)right move.
Let's say, we have r X c matrix. Let's take a simple way, all down, then all right, steps=r-1(down) + c-1 (right). Ironically every other way also takes r-1+c-1 steps.
total ways is nothing but all permutations of given way (r+c-2) => (r-1+c-1)! / (r-1)! * (c-1)! => can be re written as (m+n-2)C(n-1)
Best explanation of this problem anywhere!
Thanks :)
I think TechDose Sir has joined Google. Congratulations.
@@imshivendra What's his real name?
@@DK-ox7ze I don't know but I has seen on LinkedIn that he has joined Google
bohat shi samjhaya ...
Great explanation bro appreciate it
Simply amazing ❤
I literally loved this problem. Any way best explanation as always
I think TechDose Sir has joined Google. Congratulations.
amazing solution sir
This video is a great explanation thanks for uploading this free....
Welcome 😊
Thanks a lot sir for crystal clean explanation❣️
Welcome :)
I think TechDose Sir has joined Google. Congratulations.
great explanation. Thank you
Welcome
What is time complexity of the recursive approach?
I think TechDose Sir has joined Google. Congratulations.
thanks sir for this explaination
Welcome :)
What are the Plans for July , another Challenge or Topic-wise !
Topicwise graph algorithm.
Could we possible to print all the unique paths? If yes how it is?
The Best!
Awesome content as always :)
Just a simple query, at 4:07 I believe the recursive tree for node (1,0) in the right subtree is drawn wrong. Please correct me if I'm wrong.
I think TechDose Sir has joined Google. Congratulations.
Can anyone explain , after breaking the dptable both robot and star are at same cell(0,0) then how the number of path will be 1?
what is time complexity in recursive approach and DP ?????? why?????????
Nice, you could also solve it using combinatorics: [ (rows - 1) + (columns - 1) ] ! / [ (rows - 1) ! * (columns - 1) ! ]
I think TechDose Sir has joined Google. Congratulations.
@@imshivendra I wouldn't be surprised. He is truly one of the best CS teachers here on CZcams. The guy who runs a similar channel (NeetCode) has also started working for Google last year.
Great explanation !!!! A small doubt at 13:32 I know from line 23 its used for fastIO but can I get some explanation on it. Thank You.
The height of a conical tank is 12cm and the diameter of its base is 32cm. The cost of painting if from outside at the rate of Rs 21 per sq. cm. is
Its a cool solution but i have done it without recursion or Dp.I have done it by using binomial coefficient in which s.c=O(1)
Share your code bro :)
Excellent explanation
Thanks :)
Nice video. Most videos start from f(m-1,n-1) i.e the bottom right but I solved it starting f(0,0) just like you
I ask myself at each step : what can i do. I can either go down or go right. Then i ask what does my f(i,j) represent. In this case, it means the number of ways I can get to the bottom right starting at i and j.
This means to calculate f(i,j), i need to know the number of ways I can get to bottom right if moved right + number of ways if I move down
what is the point of dynamic programming if you are not using recursion ?
You deserve more likes sir..
Thanks :)
GREAT EXPLANATION SIR ❤👌
Thanks
I think TechDose Sir has joined Google. Congratulations.
sir i cant understand how he says x and y are 3..? 2:50
@techdose
Awesome explanation
Let's assume that the robot can move in all directions
Then how can I modify the solution?
Assume it like a graph with 4 adjacent edges and then solve :)
THANK YOU!!!!!!
Thanks
Code with time complexity O(1)
Code is like,
nCr:->
Where, n=rows + columns -2
r = columns -2
How will you calculate ncr in O(1)? I guess it ll be O(r)
You can minimise the space complexity to O(M). Following is the Golang code:
func uniquePaths(m int, n int) int {
grid := make([]int, m)
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if i == 0 || j == 0 {
grid[j] = 1
} else {
grid[j] = grid[j] + grid[j-1]
}
}
}
return grid[m-1]
}
We can optimize the space even in CPP by just maintaining a single array because we are just looking at 2 previous values :) top and left.
how is this better? if you have have an array size 5, it executes 5^2 times for the videos solution but your solution also executes 25 times.
code link doesn.t opens
👍👍
👍🏼
I tried doing Binary Tree in timestamp 4:17 myself and found that child from node (1,0) to be (1,1) & (2,0) but in the video it is given as (1,2) could you explain me how? Thanks in advance
Tha was a mistake in the video.. It must be (2, 0) instead of (1, 2)..
@@AlokDhanush-um5ol Thanks for taking time and replying :)
❤
😊
time n space complexity?
great
Thanks
Let's say, we have r X c matrix. Let's take a simple way, all down, then all right, steps=r-1(down) + c-1 (right). Ironically every other way also takes r-1+c-1 steps.
total ways is nothing but all permutations of given way (r+c-2) => (r-1+c-1)! / (r-1)! * (c-1)! => can be re written as (m+n-2)C(n-1)
Not all heroes wear caps:)
:)
Nice drawing 👌 😅
Robot is better than anything 😂🤖
Atleast someone understands art 😭
Did you get leetcode s t-shirt previous month? I think you got it. 😁😁
Are you from NIT?
It's very similar to minimum cost path
Yea :)
@@techdose4u Pls explain what is bottom up approach and top down approach
it's standard combinatorics problem
👍
how many are not from IIT/NIT/BITS ?
Me
hve anyone noticed pattern in dp table 👀
Java Solution
class Solution {
public int uniquePaths(int m, int n) {
int mat[][]=new int[n][m];
for(int i=0;i
who is feeling frustrated now ??
Why not just calculate (m+n-2)! / ( (m-1)! * (n-1)! )
Not everyone knows this formula lol
@@Moch117 it isn't a formula i used logic to covert the problem, consider m columns and n rows to get from one corner to another you need to move right (m-1) times and move down (n-1) times. The problem then becomes a permutations problem of which there are many variations. Say n is 3 and m is 4 and a down move is represented by d and a right move is represented by r. The number of permutations of rrrdd say rdrdr, rddrr etc is 5!/(3!*2!).