Unique Paths

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ต.ค. 2024

ความคิดเห็น • 147

  • @KevinNaughtonJr
    @KevinNaughtonJr  5 ปีที่แล้ว +12

    What other problems should I go over?

    • @rahulshrivastava3040
      @rahulshrivastava3040 5 ปีที่แล้ว +19

      please go through more DP problems.

    • @thekewlwaffle
      @thekewlwaffle 5 ปีที่แล้ว +3

      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
      @KevinNaughtonJr  5 ปีที่แล้ว +1

      @@thekewlwaffle I'll see what I can do thanks for the suggestion!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +1

      @@rahulshrivastava3040 You got it!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +1

      @@rahulshrivastava3040 You got it!

  • @ophir1982
    @ophir1982 4 ปีที่แล้ว +40

    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
    @midasama3124 4 ปีที่แล้ว +2

    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
      @KevinNaughtonJr  4 ปีที่แล้ว +1

      Miguel Manchola if you help with these kinds of problems be sure to subscribe to The Daily Byte! thedailybyte.dev/

  • @ghost_busters
    @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
    @MorisonMs 5 ปีที่แล้ว +6

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

  • @adampzakaria
    @adampzakaria 2 ปีที่แล้ว +4

    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.

  • @raihanhaider5649
    @raihanhaider5649 5 ปีที่แล้ว +11

    Thank you for the straight forward explanation.I'm in love with your channel

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +2

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

  • @stssports2255
    @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)

  • @yueli2146
    @yueli2146 5 ปีที่แล้ว +1

    Finally getting some intuitions on dynamic programming! Thank you. First DP question I was able to do before watching your solution lol.

  • @Sophia-fw1rm
    @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!

  • @Amy-tw3zh
    @Amy-tw3zh 4 ปีที่แล้ว +2

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

  • @DataStructures
    @DataStructures ปีที่แล้ว

    wow that actually makes a lot of sense

  • @Drake811
    @Drake811 3 ปีที่แล้ว +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
      @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))

  • @jnsmelinda
    @jnsmelinda 3 ปีที่แล้ว

    There is that nice white wall behind you. You should buy some markers to match and draw on it. :)

  • @saulgoodman980
    @saulgoodman980 4 ปีที่แล้ว +1

    It's possible to bring down the space complexity to O(n), by overwriting on the same row again and again.

  • @axbax93
    @axbax93 4 ปีที่แล้ว +5

    Thank you Kevin!! iwanna be like you when i grow up... wait.. i'm a grown ass adult 😭

  • @zacharysong4863
    @zacharysong4863 2 ปีที่แล้ว +1

    I would like to call you DP-Master.

  • @xiaoyu88
    @xiaoyu88 5 ปีที่แล้ว +1

    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!

  • @samanthkrishnamurthy
    @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 :-)

  • @galanoth17
    @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

  • @philbowden
    @philbowden 8 หลายเดือนก่อน

    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?

  • @seunghunlee6778
    @seunghunlee6778 5 ปีที่แล้ว

    Now it's clear! I can visualize it! Thanks Kevin🙏🙏

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Super happy to hear that Seunghun and anytime!!! :)

  • @vk1618
    @vk1618 4 ปีที่แล้ว

    Top-bottom approach of dymanic progamming. Which of dp problems could be solved without recursion?

  • @PrasannDeshpande
    @PrasannDeshpande 3 ปีที่แล้ว

    I've done this problem before. But today I feel like I can do this problem even after decades if asked

  • @algorithmimplementer415
    @algorithmimplementer415 5 ปีที่แล้ว +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
      @kunal_chand 4 ปีที่แล้ว

      I am thinking of using backtracking to print every unique path.
      What are your thoughts?

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

  • @AidarMHTV
    @AidarMHTV 3 ปีที่แล้ว

    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.

  • @kamalhm-dev
    @kamalhm-dev 5 ปีที่แล้ว +1

    This took me hours to understand but finally I got it!

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

  • @jocavuleta
    @jocavuleta 5 ปีที่แล้ว +1

    Awesome and it does.
    True subscribers will know the satisfaction in those words... 😃

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

  • @galanoth17
    @galanoth17 2 ปีที่แล้ว

    Can't we use dfs(right), dfs(down) recursive calls. Once you get to the end, then increment the num_paths counter?

  • @ridhwaans
    @ridhwaans 3 ปีที่แล้ว

    is there a way you can memoize your solution to reach a more optimal solution?

  • @SameerSrinivas
    @SameerSrinivas 4 ปีที่แล้ว

    Great video. It helps if you can bring visual description as well to the video.

  • @BadriBlitz
    @BadriBlitz 3 ปีที่แล้ว

    Excellent explanation. You got a new subscriber.

  • @hamzaaitboutou8563
    @hamzaaitboutou8563 5 ปีที่แล้ว +8

    Can't this just be solved in O(1) with combinatorics ?

    • @ArkabandhuChowdhury
      @ArkabandhuChowdhury 4 ปีที่แล้ว +2

      The formula is (m+n)!/(m!*n!). But it's still O(n).

    • @arungopalan
      @arungopalan 4 ปีที่แล้ว +1

      No. Even if combinatorics, calculating the actual value is O(n).

    • @VishalSharma-hq5ti
      @VishalSharma-hq5ti 4 ปีที่แล้ว +1

      @@ArkabandhuChowdhury formula is (m-1+n-1)!/ (m-1)!(n-1)!

  • @FrankLi92
    @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
      @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
      @KevinNaughtonJr  5 ปีที่แล้ว

      Also thanks for the problem suggestion I'll see what I can do :)

  • @lifeofme3172
    @lifeofme3172 4 ปีที่แล้ว +1

    I didn't understand why we return dp[n-1][m-1] , other parts were clear.

    • @freddycolon8789
      @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]

  • @ssbooth
    @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?

  • @arturoislas1546
    @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!

  • @KP-jx1wy
    @KP-jx1wy 4 ปีที่แล้ว +9

    I swear whoever first thought of this is a mutant.

  • @hey_utkarshh
    @hey_utkarshh 2 ปีที่แล้ว

    What's the Time & Space Complexity ?

  • @sakshisengar7709
    @sakshisengar7709 4 ปีที่แล้ว

    You made this so easy for me thanks a lot man.

  • @pratheek5096
    @pratheek5096 4 ปีที่แล้ว

    This was the first DP question I solved
    Thanks for this approach

    • @KevinNaughtonJr
      @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!

  • @renukadolkar7288
    @renukadolkar7288 5 ปีที่แล้ว

    Keep up the amazing work Kevin! Your videos are a blessing!

  • @harshitgupta7902
    @harshitgupta7902 4 ปีที่แล้ว

    sir,can u please teach us about recursion...because i am new to programming and these topics are difficult to understand..

  • @equaco
    @equaco 5 ปีที่แล้ว +2

    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
      @KevinNaughtonJr  5 ปีที่แล้ว +2

      Thanks for the advice Equaco! I'd always thought about putting diagrams or graphics on the videos maybe I'll start doing it!

  • @darod6098
    @darod6098 4 ปีที่แล้ว

    Wow. Pretty difficult concepto explained tooooooo easily and clear. Thank you

    • @darod6098
      @darod6098 4 ปีที่แล้ว

      btw the problem link in the description is incorrect. it is this one leetcode.com/problems/unique-paths/

  • @prajmus
    @prajmus 5 ปีที่แล้ว +1

    Audio level's much better :) great video!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Woooo so happy to hear that! Thanks for letting me know Kuba :)

  • @shrimatkapoor2200
    @shrimatkapoor2200 4 ปีที่แล้ว

    I can't believe that I didn't realise that this was a DP question in a grid, damn it!!

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

  • @Niro_Hero
    @Niro_Hero 3 ปีที่แล้ว

    Kevin, you are so awesome!

  • @jigyasuprakash911
    @jigyasuprakash911 4 ปีที่แล้ว

    The made DP seems very simple.. Thank you so much sir🙏🙏

    • @KevinNaughtonJr
      @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!

  • @sanatanshrivastava3777
    @sanatanshrivastava3777 4 ปีที่แล้ว

    Is there anyway we can do this with like dfs or something?

    • @shilpaas2433
      @shilpaas2433 4 ปีที่แล้ว

      I thought it was a DFS problem

  • @annajimmy2610
    @annajimmy2610 5 ปีที่แล้ว +1

    Thanks Kevin!It is an amazing strategy to solve this problem. I tried using combinations in python, but failed. Thanks a lot

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Thanks Anna and don't worry about it it's a tough problem just keep working at it!!!

  • @sujeeshsvalath
    @sujeeshsvalath 4 ปีที่แล้ว

    Man u made it look so easy. Thank you legend

  • @shivangishukla2629
    @shivangishukla2629 4 ปีที่แล้ว

    Please make a video on how to match a string and a pattern without using

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

  • @priyankabagul494
    @priyankabagul494 4 ปีที่แล้ว

    Your are awesome, only one request just boost volume its bit low

  • @anindyapaul113
    @anindyapaul113 4 ปีที่แล้ว

    Awesome solution.. Thanks bro..

  • @djfedor
    @djfedor 5 ปีที่แล้ว +1

    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
      @KevinNaughtonJr  5 ปีที่แล้ว +4

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

  • @surajgrandhi6742
    @surajgrandhi6742 4 ปีที่แล้ว

    How the time will be 0 whether it takes O(n^2) time complexity??

    • @navdeepmandal4338
      @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!

  • @umabharati9911
    @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 :(

  • @stonecoldcold2941
    @stonecoldcold2941 4 ปีที่แล้ว

    nice one mate

  • @navdeepmandal4338
    @navdeepmandal4338 4 ปีที่แล้ว

    This really helped sir, thanks a lot!

  • @vibekdutta6539
    @vibekdutta6539 5 ปีที่แล้ว

    Happy Teachers Day Sir, appreciate your hard work and teaching!

  • @shalinsitwala
    @shalinsitwala 4 ปีที่แล้ว

    Love your videos. You literally make every question look easy.

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว

      Thanks Shalin really appreciate that!

  • @mattbialek9633
    @mattbialek9633 4 ปีที่แล้ว

    amazing videos

  • @mahmoudnasr8710
    @mahmoudnasr8710 5 ปีที่แล้ว

    you are brilliant sir!!!! please keep on going

  • @elle4556
    @elle4556 4 ปีที่แล้ว

    OMG How do you explain so well! I m still dumbstruck!

    • @KevinNaughtonJr
      @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
      @elle4556 4 ปีที่แล้ว

      @@KevinNaughtonJr Will definitely give it a try!

  • @AtoZ-jv1py
    @AtoZ-jv1py 4 ปีที่แล้ว

    thanks for your videos (great work, nice explaination);

  • @MridulManocha
    @MridulManocha 4 ปีที่แล้ว

    Can you make a video on Unique Paths III please?

    • @KevinNaughtonJr
      @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!

  • @21void5
    @21void5 4 ปีที่แล้ว

    i have learn a lot from you. thank you so much.

  • @shwetadk1986
    @shwetadk1986 5 ปีที่แล้ว

    Thats was so easy !! You are so clear in your explainations. Thanks :)

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      Thanks for the kind works Shwetz and anytime! :)

  • @mallikakejriwal3729
    @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
      @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

  • @nishatsayyed8326
    @nishatsayyed8326 5 ปีที่แล้ว

    I am loving this channel ♥️

  • @tarankaranth8782
    @tarankaranth8782 2 ปีที่แล้ว

    hard to visualize. explanation with pic would have helped

  • @ankeetmaini
    @ankeetmaini 5 ปีที่แล้ว +1

    Can you do reconstruct itinerary in leetcode using dfs and backtracking? Thanks!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      I'll see what I can do thanks for the suggestion!

  • @kenwang5113
    @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)

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 4 ปีที่แล้ว

    Plz use paper or paint to explain

  • @rahulratra6672
    @rahulratra6672 4 ปีที่แล้ว

    Sir thanks a lot . You are doing a great job.

  • @dumitruchicu5907
    @dumitruchicu5907 2 ปีที่แล้ว

    The key question to the sub-problem definition & as well as the solution is @ #4:37

  • @Mal-wk3uq
    @Mal-wk3uq 5 ปีที่แล้ว

    How did you become good at solving problems ?this what I struggle with

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +2

      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
      @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
      @KevinNaughtonJr  5 ปีที่แล้ว

      @@Mal-wk3uq Anytime and don't worry I will! I would recommend cracking the coding interview and the elements of programming interviews!

  • @saifullahtappalmuhammad4483
    @saifullahtappalmuhammad4483 5 ปีที่แล้ว

    Jump Game (LeetCode). Please solve this question, using DP. Thanks.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว +1

      I'll see what I can do thanks for the suggestion!

  • @nareshbabu9235
    @nareshbabu9235 5 ปีที่แล้ว

    Hi Kevin Could you please upload a solution video on LeetCode 711 - Number of Distinct Islands II Problem ?

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 ปีที่แล้ว

      I'll see what I can do, thanks for the suggestion Naresh!

  • @mahmoudnasr8710
    @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 ?

  • @aronblanche
    @aronblanche 2 หลายเดือนก่อน

    Thanks

  • @poonamyadav-qz7yt
    @poonamyadav-qz7yt 4 ปีที่แล้ว

    why are you returning dp[m-1][n-1]? Why not dp[m][n] ?

  • @mr.mystiks9968
    @mr.mystiks9968 4 ปีที่แล้ว +1

    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
      @KevinNaughtonJr  4 ปีที่แล้ว +1

      Mr. Mystiks you’re welcome to leave the optimized space solution here

  • @mattbettinson4576
    @mattbettinson4576 4 ปีที่แล้ว

    Actual link: leetcode.com/problems/unique-paths/