Unique Paths | Dynamic programming | Leetcode #62

Sdílet
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...

Komentáře • 149

  • @saurabhsomani9188
    @saurabhsomani9188 Před 4 lety +98

    Somebody give this man a medal ASAP. Crystal clear explanations on each and every problem. Very Inspiring and Helpful.

    • @techdose4u
      @techdose4u  Před 4 lety +7

      🤣 Hahahahaa.....Thanks for appreciating :D

  • @VrickzGamer
    @VrickzGamer Před 3 lety +10

    I like Your Dark theme in the videos which gives my eyes the power to view more.

  • @thiyagarajanr225
    @thiyagarajanr225 Před 4 lety +17

    This man is so good at segementing the problems ! More power to you bro! A Die hard fan of you bro 💗

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Thanks :)

    • @imshivendra
      @imshivendra Před 2 lety

      I think TechDose Sir has joined Google. Congratulations.

  • @Ayush-xx1bx
    @Ayush-xx1bx Před 3 lety

    Can't thank you enough, I was stuck for hours with this approach. Now I understand it completely.

    • @imshivendra
      @imshivendra Před 2 lety

      I think TechDose Sir has joined Google. Congratulations.

  • @dumbcurious450
    @dumbcurious450 Před 3 lety +4

    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 ...

  • @rutachaudhari5774
    @rutachaudhari5774 Před 3 lety +3

    You are way underrated for your work. Thanks for the solutions!

  • @UHemanthaKumar
    @UHemanthaKumar Před 3 lety +5

    Most underrated channel ! Hatsoff to you brother !

  • @md_aaqil8027
    @md_aaqil8027 Před 4 lety +6

    Mind-blowing Explanation
    I am a fan of the way you think and solve the problem bro

    • @imshivendra
      @imshivendra Před 2 lety +1

      I think TechDose Sir has joined Google. Congratulations.

  • @lavanyam3224
    @lavanyam3224 Před 3 lety

    This universe needs people like him!! Protect him :)

  • @shobhitbajaj9667
    @shobhitbajaj9667 Před 4 lety +4

    Thank you so so much
    You have such a great talent ❤️ of explaining complex things like piece of cake.

  • @ashishupadhyay6881
    @ashishupadhyay6881 Před 4 lety +2

    Start watching at 10:40. Great stuff btw, thanks for this.

  • @aminumado6973
    @aminumado6973 Před 2 lety

    Best explanation for the unique paths problem.

  • @sudhanshukumar1558
    @sudhanshukumar1558 Před 4 lety +3

    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.

    • @techdose4u
      @techdose4u  Před 4 lety +6

      I had shown bottom up in dungeon game and so I took top down this time XD

    • @CostaKazistov
      @CostaKazistov Před 2 lety

      Top-down DP runs slightly faster (for some reason)

  • @vaishnavinatarajan3987
    @vaishnavinatarajan3987 Před 10 měsíci

    Simple solution and awesome explanation ! Too helpful.

  • @rahulbawa3969
    @rahulbawa3969 Před 3 lety +5

    Its been 10 months and this Guy still hasn't received a medal!!!! You deserve it man! Thanks for explaining this nicely.

  • @adityagoswami6881
    @adityagoswami6881 Před 3 lety +5

    The best explanation,you deserve 1 Million subscriber .

    • @techdose4u
      @techdose4u  Před 3 lety +1

      Thanks ❤️

    • @imshivendra
      @imshivendra Před 2 lety

      I think TechDose Sir has joined Google. Congratulations.

  • @MrMarchador
    @MrMarchador Před rokem

    Very precise, clear, crisp explanation.

  • @kunalsoni7681
    @kunalsoni7681 Před 4 lety +3

    Really you make this problem very easy to understand 😊😇.. thanking you sir 😊

  • @oqant0424
    @oqant0424 Před 2 lety

    Ur channel is a goldmine

  • @ajaywadhwa3398
    @ajaywadhwa3398 Před 3 lety +3

    What a superb explanations !! Really helped a lot.

  • @hariprakash4237
    @hariprakash4237 Před rokem +2

    Sucessfully completed. Thanks for this workout

  • @agileprogramming7463
    @agileprogramming7463 Před 4 lety +1

    One of the easiest DP problems 😅
    Also the Robot in thumbnail is 😍

  • @maansi3968
    @maansi3968 Před 3 lety +2

    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))

  • @PriyankaSingh-pn8xv
    @PriyankaSingh-pn8xv Před 3 lety

    Amazing explanation, thanks

  • @dhanashreegodase4445
    @dhanashreegodase4445 Před 3 lety +4

    very helpful to me! thanks alot..You deserve many more likes

  • @sathyamoorthy2888
    @sathyamoorthy2888 Před 11 měsíci

    Iam grateful to your service sir

  • @savvy9431
    @savvy9431 Před 3 lety +3

    This is how a coding problem should be explained.............. Everybody needs to learn from this man right here.!!!!!! Hats off🎉🎉

  • @TONETHAJI
    @TONETHAJI Před rokem

    Great explaination

  • @venkateshmusuvathi8468
    @venkateshmusuvathi8468 Před 3 lety +1

    Salute to ANYONE who gets this interview question and solves it within half n hr

    • @imshivendra
      @imshivendra Před 2 lety

      I think TechDose Sir has joined Google. Congratulations.

  • @vibekdutta6539
    @vibekdutta6539 Před 4 lety

    Awesome, Sir what will be the complexity of we use combinatorics for the solution, I think there is a direct solution using combinatorics

  • @rogerknight8447
    @rogerknight8447 Před 4 lety +8

    By combinatorics its like
    (m+n-2)C(n-1). An better SC approach though!!

    • @tecocode6070
      @tecocode6070 Před 4 lety +2

      Roger Knight how you explain this approach?

    • @sulekhakumari-hs4gy
      @sulekhakumari-hs4gy Před 4 lety +2

      @@tecocode6070 this problem is eqivalent to permute (m-1) down move and (n-1)right move.

    • @saitejdandge2315
      @saitejdandge2315 Před 3 lety

      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)

  • @DK-ox7ze
    @DK-ox7ze Před 3 lety +3

    Best explanation of this problem anywhere!

    • @techdose4u
      @techdose4u  Před 3 lety

      Thanks :)

    • @imshivendra
      @imshivendra Před 2 lety

      I think TechDose Sir has joined Google. Congratulations.

    • @DK-ox7ze
      @DK-ox7ze Před 2 lety

      @@imshivendra What's his real name?

    • @imshivendra
      @imshivendra Před 2 lety

      @@DK-ox7ze I don't know but I has seen on LinkedIn that he has joined Google

  • @shubhammishra1225
    @shubhammishra1225 Před 2 lety +1

    bohat shi samjhaya ...

  • @nandk45523
    @nandk45523 Před 2 lety

    Great explanation bro appreciate it

  • @ashutoshsinghrajawat6440

    Simply amazing ❤

  • @ismail8973
    @ismail8973 Před 3 lety

    I literally loved this problem. Any way best explanation as always

    • @imshivendra
      @imshivendra Před 2 lety

      I think TechDose Sir has joined Google. Congratulations.

  • @ALEEMKHAWAR1
    @ALEEMKHAWAR1 Před 2 lety

    amazing solution sir

  • @yatri6329
    @yatri6329 Před 2 lety +1

    This video is a great explanation thanks for uploading this free....

  • @Tarunkumar-si4im
    @Tarunkumar-si4im Před 3 lety +3

    Thanks a lot sir for crystal clean explanation❣️

    • @techdose4u
      @techdose4u  Před 3 lety +1

      Welcome :)

    • @imshivendra
      @imshivendra Před 2 lety

      I think TechDose Sir has joined Google. Congratulations.

  • @275phuongvy
    @275phuongvy Před 3 lety +1

    great explanation. Thank you

  • @gaunikasrivastava7851
    @gaunikasrivastava7851 Před 3 lety

    What is time complexity of the recursive approach?

  • @imshivendra
    @imshivendra Před 2 lety

    I think TechDose Sir has joined Google. Congratulations.

  • @noobCoder26
    @noobCoder26 Před 3 lety +1

    thanks sir for this explaination

  • @acxd
    @acxd Před 4 lety +2

    What are the Plans for July , another Challenge or Topic-wise !

  • @shyamprakashm6325
    @shyamprakashm6325 Před 3 lety

    Could we possible to print all the unique paths? If yes how it is?

  • @thegreekgoat98
    @thegreekgoat98 Před 2 lety

    The Best!

  • @amanbarnwal6668
    @amanbarnwal6668 Před 3 lety +1

    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.

    • @imshivendra
      @imshivendra Před 2 lety

      I think TechDose Sir has joined Google. Congratulations.

  • @vijayj1997
    @vijayj1997 Před 3 lety

    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?

  • @bisatisrilatha7957
    @bisatisrilatha7957 Před 3 lety

    what is time complexity in recursive approach and DP ?????? why?????????

  • @bendesmedt9036
    @bendesmedt9036 Před 3 lety +3

    Nice, you could also solve it using combinatorics: [ (rows - 1) + (columns - 1) ] ! / [ (rows - 1) ! * (columns - 1) ! ]

    • @imshivendra
      @imshivendra Před 2 lety +1

      I think TechDose Sir has joined Google. Congratulations.

    • @CostaKazistov
      @CostaKazistov Před 2 lety

      @@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.

  • @dontgiveup5271
    @dontgiveup5271 Před 2 lety +1

    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.

    • @imshivendra
      @imshivendra Před 2 lety

      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

  • @ShubhamMahawar_Dancer_Actor

    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)

  • @SuganthanMadhav
    @SuganthanMadhav Před 4 lety +1

    Excellent explanation

  • @Moch117
    @Moch117 Před 8 měsíci

    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

  • @kamele0278
    @kamele0278 Před rokem

    what is the point of dynamic programming if you are not using recursion ?

  • @manishmundra8902
    @manishmundra8902 Před 3 lety +1

    You deserve more likes sir..

  • @nileshsinha7869
    @nileshsinha7869 Před 3 lety +1

    GREAT EXPLANATION SIR ❤👌

  • @snehan3341
    @snehan3341 Před 3 lety

    sir i cant understand how he says x and y are 3..? 2:50

  • @prasantharavindramesh1078

    @techdose
    Awesome explanation
    Let's assume that the robot can move in all directions
    Then how can I modify the solution?

    • @techdose4u
      @techdose4u  Před 3 lety

      Assume it like a graph with 4 adjacent edges and then solve :)

  • @brandoncontreras343
    @brandoncontreras343 Před 3 lety +1

    THANK YOU!!!!!!

  • @NikhilKumar-of5gb
    @NikhilKumar-of5gb Před 4 lety +1

    Code with time complexity O(1)
    Code is like,
    nCr:->
    Where, n=rows + columns -2
    r = columns -2

    • @Rahul-rp5hk
      @Rahul-rp5hk Před 4 lety

      How will you calculate ncr in O(1)? I guess it ll be O(r)

  • @RohitSubedi
    @RohitSubedi Před 4 lety +1

    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]
    }

    • @techdose4u
      @techdose4u  Před 4 lety +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.

    • @rubinluitel158
      @rubinluitel158 Před 4 lety

      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.

  • @backtobasics6865
    @backtobasics6865 Před 3 lety

    code link doesn.t opens

  • @Tarunkumar-si4im
    @Tarunkumar-si4im Před 3 lety +1

    👍👍

  • @HARINIKUMAR-qm3jc
    @HARINIKUMAR-qm3jc Před 11 měsíci

    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

    • @AlokDhanush-um5ol
      @AlokDhanush-um5ol Před 10 měsíci

      Tha was a mistake in the video.. It must be (2, 0) instead of (1, 2)..

    • @HARINIKUMAR-qm3jc
      @HARINIKUMAR-qm3jc Před 10 měsíci

      @@AlokDhanush-um5ol Thanks for taking time and replying :)

  • @shivasaini4097
    @shivasaini4097 Před 2 lety +1

  • @shailygoyal5174
    @shailygoyal5174 Před 2 lety

    time n space complexity?

  • @muhammedjack8303
    @muhammedjack8303 Před 4 lety

    great

  • @saitejdandge2315
    @saitejdandge2315 Před 3 lety +1

    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)

  • @saurabhkanswal7954
    @saurabhkanswal7954 Před 4 lety +2

    Not all heroes wear caps:)

  • @shresthmishra9329
    @shresthmishra9329 Před 4 lety +2

    Nice drawing 👌 😅

    • @spetsnaz_2
      @spetsnaz_2 Před 4 lety +1

      Robot is better than anything 😂🤖

    • @techdose4u
      @techdose4u  Před 4 lety +7

      Atleast someone understands art 😭

  • @ASocial093
    @ASocial093 Před 4 lety

    Did you get leetcode s t-shirt previous month? I think you got it. 😁😁

  • @VrickzGamer
    @VrickzGamer Před 3 lety

    Are you from NIT?

  • @E__ShameemahamedS-bx2ed
    @E__ShameemahamedS-bx2ed Před 3 lety +1

    It's very similar to minimum cost path

  • @DreaMagnifier
    @DreaMagnifier Před 4 lety +1

    it's standard combinatorics problem

  • @VrickzGamer
    @VrickzGamer Před 3 lety +1

    how many are not from IIT/NIT/BITS ?

  • @krishnakantsharma6161
    @krishnakantsharma6161 Před 3 lety

    hve anyone noticed pattern in dp table 👀

  • @md_aaqil8027
    @md_aaqil8027 Před 4 lety

    Java Solution
    class Solution {
    public int uniquePaths(int m, int n) {
    int mat[][]=new int[n][m];
    for(int i=0;i

  • @_Rich_YT_
    @_Rich_YT_ Před 2 lety

    who is feeling frustrated now ??

  • @jeantharappel6633
    @jeantharappel6633 Před rokem +1

    Why not just calculate (m+n-2)! / ( (m-1)! * (n-1)! )

    • @Moch117
      @Moch117 Před 8 měsíci

      Not everyone knows this formula lol

    • @jeantharappel6633
      @jeantharappel6633 Před 7 měsíci

      @@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!).