Algorithms: Memoization and Dynamic Programming

Sdílet
Vložit
  • čas přidán 26. 09. 2016
  • Learn the basics of memoization and dynamic programming. This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell.
    www.hackerrank.com/domains/tut...
  • Věda a technologie

Komentáře • 368

  • @zingg7203
    @zingg7203 Před 7 lety +214

    0:00 Gayle Laakmann McDowell
    0:14 Example Fibonacci's
    4:26 Example Maze
    Dynamic programming:
    breaking-down into subroutines;
    store/memorize subroutines;
    reuse subroutine results.

  • @SteversIO
    @SteversIO Před 6 lety +47

    Bought the books years ago. Just stumbled onto this CZcams channel. Glad I did!

    • @tejasp7700
      @tejasp7700 Před 4 lety

      How does the program stop at 0 , why does it not go to negative values

    • @videovideoguy
      @videovideoguy Před 3 lety

      Good!!

  • @ujjvalsharma5055
    @ujjvalsharma5055 Před 4 lety +21

    This is an amazing video. I request to post more videos on dynamic programming (memory+recursion). This video was very helpful. keep up the good work.

  • @axandermorales
    @axandermorales Před 5 lety +14

    Thank you so much this is a magnificent explanation. Super clear I was able to write the code and ut works perfectly. I don't mind the typos it is clear to understand despite those. Thanks!!

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

    Thank you very much for the explanation. I was solving the unique paths problem couple of days ago and I was getting exponential time while submitting the answer. Never realized that we could memorize the repetition of work.

  • @ShivangiSingh-wc3gk
    @ShivangiSingh-wc3gk Před 5 lety +4

    Loved the Maze example. Thank You!

  • @dmitrys256
    @dmitrys256 Před 7 lety +499

    at 04:00 for mem version
    Shouldn't be like below?
    else if (!memo[n]) memo[n] = fib(n-1, memo) + fib(n-2, memo);

    • @raulnunez7580
      @raulnunez7580 Před 7 lety +26

      Exactly what I was thinking.

    • @alexqiu3782
      @alexqiu3782 Před 7 lety +16

      I am 100% agree with you

    • @MauriceMickens
      @MauriceMickens Před 7 lety +7

      I agree Dmitry. I'm checking for negative values instead in Swift.
      static func fib(n:Int, memo:inout [Int])->Int{
      if n

    • @xalspaero
      @xalspaero Před 7 lety +8

      yep, it's a bug =)

    • @TheSportstar77
      @TheSportstar77 Před 7 lety +11

      Would if(!memo[n]) mean If memo[n] hasn't been created yet?

  • @jialiliang3606
    @jialiliang3606 Před 2 lety +8

    This is so good! She concises my 2.5 hrs lecture into 11 mins.

    • @trollface1994
      @trollface1994 Před 5 měsíci

      i can EXACTLY agree with this. 3 hour lecture for MSc CS class in 11 minutes.

  • @jc_alpha
    @jc_alpha Před 5 lety +5

    You say “the reason is THAT...” instead of the more common and painful “the reason is BECAUSE...” 😍😍😍
    Thank you!!!!
    That alone makes this video awesome!

  • @b4singularity
    @b4singularity Před 4 lety +5

    I definitely agree with "use rows and columns vs x and y"

  • @Seborgium26
    @Seborgium26 Před 3 lety +8

    1:44 Notice how the frequency of each item other than 0 forms the Fibonacci sequence.
    fib(6): 1 occurrence
    fib(5): 1 occurrence
    fib(4): 2 occurrences
    fib(3): 3 occurences
    fib(2): 5 occurences
    fib(1): 8 occurences

  • @Bodyja
    @Bodyja Před 4 lety +9

    I've undertood in 10 min something I did not in university. Good job :D

  • @stanst2755
    @stanst2755 Před 2 lety +2

    Thanks for the interesting video. Very concise and clear explanation of dynamic programming concept.

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

    Dynamic programming problems are the hardest to grasp for me, but this was really beneficial, especially for problems involving DFS

  • @RegularTetragon
    @RegularTetragon Před 6 lety +11

    Is it a coincidence that the frequency of fib(n) for the particular n at 1:59 follows the Fibonacci sequence?

  • @Irwansyah-kq8lf
    @Irwansyah-kq8lf Před rokem +1

    Amazing, very good explanation, makes me understand the concept of memoization, thanks!

  • @lepakshi123
    @lepakshi123 Před 3 lety

    Best as always, Gayle Laakmann.

  • @hueguy
    @hueguy Před 3 lety

    Thank you for making it so easy to understand !

  • @kidou123456
    @kidou123456 Před 7 lety +12

    Though it's a really helpful tutorial of DP (Thanks to Gayle), I think the memoization solution of maze problem misses a parameter in the recursive call. The correct code should be:
    int countPaths(boolean[][] grid, int row, int col, int[][] paths) {
    if (!isValidSquare(grid, row, col)) return 0;
    if (isAtEnd(grid, row, col)) return 1;
    if (paths[row][col] == 0) {
    paths[row][col] = countPaths(grid, row + 1, col, paths) + countPaths(grid, row, col + 1, paths);
    }
    return paths[row][col];
    }
    In this case, the function will be able to check the paths 2-D array to retrieve the result already calculated.
    Looking forward to your feedback!

  • @ififif31
    @ififif31 Před 7 lety +7

    Notice that her underlined if statement (at 7:21) can still unnecessarily call the countPaths function multiple times because the zero value it is checking for could have come from either the INITIAL zero value (originally stored in the paths matrix), or a CALCULATED zero value returned from countPaths. Simple initializing all the values in the paths matrix to -1 at the beginning and then checking for a -1 (instead of zero) in that same if statement will fix that.

    • @isaacdouglas1119
      @isaacdouglas1119 Před 3 lety

      True dat

    • @davidyang5131
      @davidyang5131 Před 2 lety

      @@isaacdouglas1119 Why are there so many damn flaws in their code lmao how am i suppose to learn.

  • @codingwithike8887
    @codingwithike8887 Před 3 lety

    This video is extremely well done!

  • @carolinaalbamaruganrubio2446

    You are such an amazing teacher!!

  • @julienbongars4287
    @julienbongars4287 Před 5 lety

    This is a concept that I wish a lot more developers would understand...

  • @mehmetdemirel6809
    @mehmetdemirel6809 Před 7 lety +19

    At 07:22, in your DP code, you are calling the countPaths method without the fourth parameter, which is paths.

    • @michaelstueben2880
      @michaelstueben2880 Před 7 lety

      Excellent video. Thanks.

    • @RavinduKumarasiri
      @RavinduKumarasiri Před 7 lety +1

      Both the DP codes are like that. There should be a memory parameter in the function call isn't it...?

    • @keerthi71815
      @keerthi71815 Před 5 lety +1

      @@RavinduKumarasiri Yes!! it's just a typo

  • @aniruddhashevle
    @aniruddhashevle Před 3 lety

    Thank you so much, you really enlightened my way!

  • @sauragra
    @sauragra Před 6 lety

    Very well explained. Thanks.

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

    I once stumbled onto the misconseption you mentioned when using y as rows and x as columns and now I just figured it out why it happened then 😅. When we use the grid representation it would be good to indicate that arrows pointing the directions mean that its the way where row numbers are increasing and not neccessarily the way that rows are aligned I’m kind of dumb but if anyone else had this problem that might be the answer

  • @sebbes333
    @sebbes333 Před 7 lety +11

    8:26 Actually that cell is unreachable from the green guys current position, because he can't go left or up.
    Same with the 5 cells (7, 4, 1, 1,1) in the bottom left corner.
    But I assume you mean that each number represents all available paths IF the guy would have stood in that specific cell?

    • @elliotheisler1437
      @elliotheisler1437 Před 5 lety +2

      Yes those cells are unreachable, but it doesn't affect the correctness of the algorithm. The cell at 8:26 is prevented from summing into the final no. of paths because of the blocks to the top and left. A cell which is unreachable to the green guy is always a cell with blocks to the top and left. So by this logic you can convince yourself the algorithm won't sum up paths through unreachable cells.

  • @johnc4624
    @johnc4624 Před rokem

    I like the "traditional" approach at 10 mins.
    That is actually quite interesting and never thought about that

  • @sundarb6673
    @sundarb6673 Před 5 lety

    Thank you, Gayle!

  • @michaelstueben2880
    @michaelstueben2880 Před 7 lety

    Excellent video. Thanks.

  • @karimzuniverse
    @karimzuniverse Před 5 lety +4

    In the dynamic programming approach, I think we can have only O(n) space complexity, since we only need to store the values in the current row in the grid and the one below it.

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

    Great explanation! In the last example, the space complexity can be reduced to O(n) because we only need two rows or two columns of values if we sweep the matrix row by row.

    • @Grassmpl
      @Grassmpl Před 2 lety

      If you count the presence of the original grid, the space complexity with be O(n^2) regardless. If you don't count the grid, you can define the grid in a way to modify it in place and the space would be O(1).

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

      Actually even in this implementation we can make constant space. Just keep only 2x2 subgrid at each step.

  • @khalifacastaway6356
    @khalifacastaway6356 Před 2 lety

    Awesome video in illustrating difficult concepts

  • @DadBodSwagGod
    @DadBodSwagGod Před 6 lety +1

    I like the methodology for figuring out a solution, but your path traversal breaks down if you're bottlenecked by a zigzag pattern of unavailable spaces where the only way to get to the end is to go right, down, and then left before you can resume going down and to the right

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

    Why the recursive version of the second problem is O(2^(n^2))) ??

  • @blakeedwards3582
    @blakeedwards3582 Před 2 lety

    Great explanation. Thank you!

  • @charlliemurphy8381
    @charlliemurphy8381 Před 4 lety

    Reading her book now. Pretty informative.

  • @ScottGrodberg1
    @ScottGrodberg1 Před 7 lety +49

    06:50 To be clear, don't use Cartesian x and y when dealing with matrices.

    • @pchandu1995
      @pchandu1995 Před 7 lety +2

      using pairs of (x,y) will decrease the key press when implementing the algorithm rather using those matrix representations just kidding.

    • @tiny_paul
      @tiny_paul Před 7 lety +20

      This was honestly one of the most helpful tips I have ever received. When I started out programming, trying to use x and y, I wasted hours working around grid coordinates in my head.

    • @brokenjava11
      @brokenjava11 Před 7 lety +17

      so use j,k as indicies? jk.

    • @Anohaxer
      @Anohaxer Před 7 lety +1

      or just use x & y and think in Cartesian
      its not actually that hard to convert beetween rows&columns to Cartesian x&y and the other way
      and by making it cartesian you can do x,y instead of y,x because in cartesian x is usually defined before y
      or to go trought use r & c instead of row & col

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

      Label things whatever is meaningful to the situation.

  • @xiao2634
    @xiao2634 Před 7 lety

    For the last example at 7:50, can you count and fill the matrix from the top left to the bottom right?

  • @nishanthbhaktula7754
    @nishanthbhaktula7754 Před 6 lety

    is runtime Big O of N squared - as you describe it? or is it Big O of 2 to the Power of N Squared - as displayed on the screen? As I understand its the former.

  • @uknow2908
    @uknow2908 Před 2 lety

    Thanks! Very clear explanation.

  • @foolishmusic9430
    @foolishmusic9430 Před 7 lety +56

    else if (memo[n])
    Isnt this condition saying if array memo of index n is filled? but shouldn't it be !memo[n] cause we are trying to fill up the array?

    • @FreakinKatGaming
      @FreakinKatGaming Před 3 lety

      Alot of the time man it's just situational and can vary from project to project

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

      @@FreakinKatGaming what kind of bullshit answer is this. Plain and simple this code will return null if the value haven't been computed. @Foolish Music you are correct when it comes to the code that they showed.

    • @FreakinKatGaming
      @FreakinKatGaming Před 3 lety

      @@basicnamenothingtoseehere 🤣 wow you a took that seriously. Bless your little heart . Omg you made my day, ain't you just the cutest little Dickens!!!! Man my day was NULL until I read this man variables are awesome. 😅😋 Nahh but why not apt moo - around

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

      @@FreakinKatGaming shut the fuck up brother

    • @FreakinKatGaming
      @FreakinKatGaming Před 3 lety

      @@daruiraikage I'm typing not speaking, if ya gotta problem block me. That simple. Otherwise make me!

  • @abhaykumar9806
    @abhaykumar9806 Před 7 lety +1

    how can you even reach 7, 5th box top left if the above box is blocked? the search should not calculate that.. isnt it?

  • @Badboys6Reborn
    @Badboys6Reborn Před rokem +3

    In the Fibonacci example why do you do “if else memo[n]” rather than “if else !(memo[n])”? Aren’t you checking if there is a value and if there isn’t (ie it is NULL) then calculate it?

  • @jaimeduncan6167
    @jaimeduncan6167 Před 9 měsíci

    This is an excellent resource.

  • @korinostream
    @korinostream Před 7 lety +1

    your concepts are banging stars!!!!! (y)

  • @videovideoguy
    @videovideoguy Před 3 lety

    Good explanation of memoization !!!

  • @mohammadzeqlam5098
    @mohammadzeqlam5098 Před 7 lety

    at 10:24...
    the first column to the left after the pink square all have 0 paths; because you can only go to the right and down only so you can't go left .

  • @luis-azanza
    @luis-azanza Před 7 lety +9

    Thank you so much Gayle!

    • @shamassive
      @shamassive Před 7 lety +1

      Upvoting this as the only positive and thankful answer in the entire comments thread :P

    • @luis-azanza
      @luis-azanza Před 7 lety

      shamassive thanks a lot! It was helpful to me.

  • @elliota8180
    @elliota8180 Před 9 měsíci

    Thanks to this lady, the entire interview process is in the gutter! Congratulations on single-handedly destroying the creative interviewing process and turning it into a graded exam!

    • @cosmic_gate476
      @cosmic_gate476 Před 6 měsíci

      When 5000 people beg at Google's doorstep for the same position, this is the inevitable result. The winner is the person who maximizes their ability to game heavily standardized interview processes. All this lady did was level the playing field and make heaps of money off it.

  • @swapnik1000
    @swapnik1000 Před 2 lety

    Amazing explanation!

  • @riyadshauk2432
    @riyadshauk2432 Před 7 lety +5

    How'd she calculate the "(simple) Recursive" runtime of O(2^{n^2})? (7:25) I think because there are n^2 possible cells (assuming the problem is run on an n by n grid), and at each cell there are a maximum of two possible moves that would add to a path: go down, or go right. By the basic principle of counting (generalized), there are 2^{n^2} possible outcomes/paths to check for existence, thus the running time is O(2^{n^2}).

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

      Nah it should be O(2^n) because we’re using Manhattan distance here. So every path from start to end is n grids, and at every grid you choose to go right or down, hence 2^n

    • @summerzeelee
      @summerzeelee Před rokem

      @@baiyuli97 I got the same answer. I drew out the call tree for N=4, and there are only 7 levels. This means there are 2N - 1 levels, and each node has 2 child nodes. So, it's O(2^n) because the constants go away. Another way to think about it is to draw out the grid and count the steps. Regardless of the order in which you traverse the tree, ultimately you will need to move N - 1 to the right and N - 1 down. If you count the start node as a step, that's 2N - 1. So, any given path will be 2N - 1 deep. I might be wrong, but I can't figure out how she got O(2^(n^2)).

  • @FlareGunDebate
    @FlareGunDebate Před 5 lety

    For purposes of conceptual symmetry when partitioning space I too use y as column and x as row. Which order x and y appear in as an index for a matrix is arbitrary (column or row major order). Preserving the idea of x as the horizon is orienting for me. Are you using some kind of projection logic to resolve the inconsistency or do you just accept the x as column without question as a best practice? Also, this is a great video. Thank you.

  • @patientlearning9303
    @patientlearning9303 Před 4 lety

    Best Teacher, thanks

  • @dev-skills
    @dev-skills Před 3 lety

    space complexity of recursive solution for fibonacci series very well explained.

  • @michaelbrooks6713
    @michaelbrooks6713 Před 5 lety

    Great job, Thanks for your sharing!!!

  • @anbansal2k
    @anbansal2k Před 3 lety

    Can the memo array be initialised with 0 and 1 and then the 2 ifs be removed?

  • @guess1985
    @guess1985 Před 7 lety +7

    How can we call a 2 parameter function with one? Shouldnt the code be more like
    else if (memo[n]) return memo[n]; ? Then another else for putting the calculation in the memo?

    • @xitrumch
      @xitrumch Před 7 lety +2

      that code is not accurate, the concept is right, but the implementation is not accurate in my opinion. You could refer to www.geeksforgeeks.org/program-for-nth-fibonacci-number/ for the correct implementation

    • @DailtonR
      @DailtonR Před 7 lety

      Yeah this stumped me for a minute until I realized it's not correct

    • @xitrumch
      @xitrumch Před 7 lety +3

      Actually she was right, it just a bit difficult to understand without seeing the full implementation. You guys can follow her full implementation here: github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2008.%20Recursion%20and%20Dynamic%20Programming/Q8_01_Triple_Step/QuestionB.java
      public static int countWays(int n) {
      int[] map = new int[n + 1];
      Arrays.fill(map, -1);
      return countWays(n, map);
      }
      public static int countWays(int n, int[] memo) {
      if (n < 0) {
      return 0;
      } else if (n == 0) {
      return 1;
      } else if (memo[n] > -1) {
      return memo[n];
      } else {
      memo[n] = countWays(n - 1, memo) + countWays(n - 2, memo) + countWays(n - 3, memo);
      return memo[n];
      }
      }

    • @RajeevSoni007
      @RajeevSoni007 Před 7 lety

      @Kutay Kalkan yes , it should return memo[n] if memo[n] is positive.
      or it can be the negation in her case . if it was missed.

  • @starblazer13
    @starblazer13 Před 7 lety +5

    If you can only move right and down, then the path on the left most starting at 7 [4][0] to [7][0] downward including that 1 [7][1] is impossible. Same with the 2 [6][6] right at the end. You just cannot get to those squares.

    • @QuiqueZapata
      @QuiqueZapata Před 7 lety +1

      Yes, also having a matrix that looks like this (where 0 means empty space, X means blocked, S start and E end) would be impossible, when there is clearly a path:
      S X 0 0 0
      0 X 0 X 0
      0 0 0 X E

    • @medievalogic
      @medievalogic Před 6 lety

      Yes but what if you were spawned that those squares ? The number suggests how many paths are there if you start from there.

  • @Cendrounet
    @Cendrounet Před 7 lety

    Hello, What is the software used to draw ?

  • @nekososu
    @nekososu Před 6 lety

    what is the software you use for drawing?

  • @rovsenhuseynov8368
    @rovsenhuseynov8368 Před 4 lety

    very useful video. Thanks

  • @Glicerol
    @Glicerol Před 5 lety

    What about loops? Why do you assume you can only go to the bottom or right?

  • @xavierj186
    @xavierj186 Před 4 lety

    This was easy to follow and understand. Thank you

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

    Great video, can I ask what tools did you use for the graphics and drawings ?

    • @mayankgupta2543
      @mayankgupta2543 Před 4 lety

      Isn't Can i ask technically a glitch, when you have already asked what you wanted to ask when asking for the permission to ask.

    • @stevenwilt4450
      @stevenwilt4450 Před 2 lety +2

      @@mayankgupta2543 Only if you read everything literally, which is misunderstanding the language.

  • @salmark9080
    @salmark9080 Před 4 lety

    is any of this useful for trading, anyone know?

  • @ayushigupta4041
    @ayushigupta4041 Před 4 lety

    Which software are they using for writing the text in the video?

  • @hacker-7214
    @hacker-7214 Před 4 lety +3

    7:56 how did you figure out what the recursion approach does? Like i can't even wrap my head around the recursive tree especially at the end .. please reply

  • @Tiecongli
    @Tiecongli Před 6 lety +5

    Recursion approach with just down/right direction wont' work, you have to try all directions for some cases. But the second DP one brilliant

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

      Had the same thought process. Happy that I'm not alone.

  • @AndrewMelnychuk0seen
    @AndrewMelnychuk0seen Před 6 lety

    This was great!

  • @CyberMew
    @CyberMew Před 3 lety

    So what was the last part going from bottom-up about? Is it just running the example or showing us a different way to do code it?

  • @abdoulbarry8111
    @abdoulbarry8111 Před 2 lety

    Best videos eveeerrr!! Matrix problems I just use i and j.

  • @mickeykutti5165
    @mickeykutti5165 Před 2 lety

    What do the words associative arrays , lookup table,cache hit/miss ratio have in common with memoization?

  • @DragonStoneCreations
    @DragonStoneCreations Před 4 lety

    Is this a Breadth First Traversal ?

  • @felidon
    @felidon Před 4 lety

    For the path problem, dp solution's space complexity is actually 2(n^2) but it is admissible as (n^2)
    And we don't fully get rid of call stack since the method is still recursive. But since we store the values, our call stack grows up to a certain degree that is negligible.
    Does that what she mean at the end of the video ?

    • @Bubdiddly
      @Bubdiddly Před 2 lety

      Might be misunderstanding your comment but I think it’s because she’s talking big O so the 2 * n^2 is reduced to n^2?

  • @frankbraker
    @frankbraker Před 5 lety +7

    7:37 Can anyone tell me why is the simple approach O(2^(n^2)) please?

    • @g01dHaCkEr
      @g01dHaCkEr Před 5 lety +12

      That problem is fairly similar to the fibonacci problem, and you can model it in the same way as a binary tree. It's binary because we call the function recursively twice in each iteration. Since you have to traverse the entire tree, then we have O(2^(time complexity for one cell)). The algorithm to compute the number of paths from any cell to the destination for the simple, brute-force algorithm is O(n^2), since it has to traverse the whole grid and sum up paths as it goes. Remember, this is with no memoization, so it does the same work over and over again. Therefore, the final time complexity is O(2^(n^2)). It's a good example of how much better the memoized version is. It only has to traverse the grid once. The rest is just constant time lookups.

    • @VigneshDhakshinamoorthy
      @VigneshDhakshinamoorthy Před 3 lety

      First case, with recursion, we run two parallel paths to find the distance. Move Right, Move Down (total 2 counts) times all the boxes. Hence two times N^2.
      In second approach, If there is a 10x10 maze, we have to run the loop 100 times to fill the boxes in the matrix and find the answer. Hence it is just N^2.
      If we can memoize, which is to store the paths in a map, then it will save some time cost and bring it down to N^2 for the recursion approach itself.

    • @thegreatlazydazz
      @thegreatlazydazz Před 3 lety

      I dunno, for the case of non-memonization: I believe it is 2^(2n) calls to the function. If you draw the recursion tree, at every level the number of nodes doubles.
      Now we need to find the depth of the tree. When we go down the tree either the x coordinate or y coordinate increases. Both these numbers can only increase up to n, so in 2n calls we will be at the destination.
      2n depth means number of calls is \sum_{i=1 to 2n} 2^i = 2^(2n)

  • @avatar098
    @avatar098 Před 7 lety +5

    I have a bachelors in Computer Science and this still has been incredibly helpful in keeping my fundamentals sharp!

  • @deputyVH
    @deputyVH Před 5 lety

    I didn't understand the iterative approach. What if you need to back track or side track? Then the number of paths are increased.

  • @bprincepandey
    @bprincepandey Před 2 lety

    Thanks for this video.

  • @kitgary
    @kitgary Před 7 lety

    Why is the space complexity for the non-memorised solution is O(n)? I can't figure it out, can someone explain?

    • @neilpradhan1312
      @neilpradhan1312 Před 4 lety

      The space complexity is the maximum space a program takes at a particular time, as at a time instant "t" the stack has only n functions to perform and as at new time instant "t+1" one function is executed so removed from the stack and new function is added so net n functions only remain at a particular instant hence it is O(n)

  • @Kimberly-bk8vx
    @Kimberly-bk8vx Před 7 lety +1

    This is great. :)

  • @jorgeherrera1074
    @jorgeherrera1074 Před 7 lety

    How does that code handle the case where it has zero paths to the end? Would that not cause compilation or run-time error?

    • @AsaGayle
      @AsaGayle Před 7 lety

      It has a validation function that checks whether or not the path down or right are possible paths and returns 0 instead of 1. The other squares are pretty much made up by adding the values of the squares after its right and below it in the call stack till you hit the base case (you reaching the end which returns 1) so it would just be a square with a value of 0 and add 0 to any square above it or to its left, hence why the square above it equals 1 rather than 2.

  • @swapnik1000
    @swapnik1000 Před 2 lety

    Wouldn't this break if there were enough blocks requiring the path to go up or left?

  • @e.g.4483
    @e.g.4483 Před 3 lety

    Would have been nice to see the explanation of adding in the memo array

  • @Zachi9
    @Zachi9 Před 7 lety +3

    What Kind of Illustration program is she using for the presentation?

    • @TylerShawful
      @TylerShawful Před 6 lety

      I want to know this as well, doing a quick google search didnt come up with much

    • @amateruss
      @amateruss Před 6 lety

      Paint

  • @skellep
    @skellep Před 4 lety

    when I do return countPaths(grid, row + 1, col) + countPaths(grid, row, col + 1); I get index out of bound how / why do you not account for this? what am I missing? Thanks!

    • @ua9091
      @ua9091 Před 3 lety

      Maybe that was supposed to be taken care in the validSquare function.

  • @isaacpak1628
    @isaacpak1628 Před 7 lety +1

    Could someone explain the condition of paths[row][col] == 0 in the memoization solution?

    • @SoReaICru
      @SoReaICru Před 7 lety +1

      With a memoization solution, the goal is to eliminate the need to repeat calculations that have already been calculated. Initially, the solution could be broken down from path(start, end) = path(A, end) + path (B, end). These separate ones can be broken down (recursively) even further: path(A, end) = path(D, end) + path (C, end) and path(B, end) = path(C, end) + path (E, end).
      See here, path(C, end) is already repeated. So passing in an additional array, in this case paths[][] can store the values that have been calculated.
      The condition is thus necessary to check before further calculating. If paths[row][col] already has a value, then just return the value. If paths[row][col] is 0, then then value has not been calculated yet, so calculate it.

  • @SS-iz9vo
    @SS-iz9vo Před 4 lety +2

    at 2:14 the memoization code for Fibonacci is incorrect. This would be the fix (pink line of code)-
    else if (!memo[n]) {
    memo[n] = fib(n-1, memo) + fib(n-2, memo);
    }
    In Python:
    def fib(n, memo):
    if n==0:
    memo[n] = 0
    return 0
    elif n==1:
    memo[n] = 1
    return 1
    elif not n in memo:
    print(n)
    memo[n] = fib(n-1, memo) + fib(n-2, memo)
    return memo[n]
    print( fib(5, {}))

  • @VaibhavSharma-zj4gk
    @VaibhavSharma-zj4gk Před 2 lety +3

    Point to be noted- We will never reach sqaures with 7,4,1,1 in first column, as we can only go right but not left.

    • @JeffPohlmeyer
      @JeffPohlmeyer Před 2 lety

      So I wasn’t sure about that. The way she coded it up, you would never hit the square with 7 paths, but that is a valid path which leads me to believe that the algorithm was slightly incomplete, no? I mean, that _should_ be a valid path, but if we have checking left and right in the same recursion then we would never leave the loop. How would one handle that scenario?

    • @SergioGomez-qe3kn
      @SergioGomez-qe3kn Před rokem

      @@JeffPohlmeyer Yeah, there are two ifs missing before the recursive calls. A recursive call can only be done if the adjacent cell is obstacle free.

  • @tomyang7788
    @tomyang7788 Před 6 lety

    at 7:30 why do we have to check if paths [row][col] == 0 ?

    • @amateruss
      @amateruss Před 6 lety

      To check whether or not there has been a stored calculated path on that specific row and column. This makes the program not use unnecessary steps to calculate a previously calculated path. Thus, it lowers the run-time of the program.

  • @samyukthamobile8447
    @samyukthamobile8447 Před 4 lety

    Understood the memorization approach in case of matrices.

  • @okeuwechue9238
    @okeuwechue9238 Před 5 lety

    How does this algorithm deal with walls that force the user to retrace their steps (in order to get out from a dead end)?

    • @g01dHaCkEr
      @g01dHaCkEr Před 5 lety

      It doesn't. In order to retrace your steps, you would need to either travel left, or up. Which was not allowed according to the problem definition.

  • @video-vocabulary
    @video-vocabulary Před 3 měsíci +1

    Which software this presentation was created with?

  • @tesukim4338
    @tesukim4338 Před 4 lety

    Are the background books sponsors? '코딩인터뷰 완전분석' It doesn't seem like the Korean book is there for you to read.

  • @davidbarth80
    @davidbarth80 Před 7 lety +5

    any good books that is leading one into problemsolving and problemsolving techniques? Cheers

  • @tomyang7788
    @tomyang7788 Před 6 lety

    what is the fourth parameter int[][] paths mean at 7:30 ?

    • @RmaxTwice
      @RmaxTwice Před 6 lety +1

      Hello, that fourth parameter is an integers matrix that holds the number of paths each cell has towards the end and we check if paths [row][col] == 0 because I assume they initialized it as a matrix full of zeroes, so if it has a 0, then it hasn't been computed yet and you need to memorize it.

  • @GPCTM
    @GPCTM Před 7 lety

    n=int(input("how many do you want?"))
    x,y = 1,1
    for i in range(1, n):
    print(x)
    x,y = y, x+y

  • @videofountain
    @videofountain Před 7 lety

    Is this code correct? I do not happen to know what language or pseudo language is intended. Written in video .... if (memo[n]). Should that be corrected to ... if (memo[n] < 0) ? .. It seems you would update the value if it does not have a value > 0. The default value is not specified. Should the video be annotated?

  • @rydmerlin
    @rydmerlin Před rokem

    What is the purpose of your if test for memo[n]. You left out the == 0 .Don’t you only want to compute it if it’s not cached and return the one you have cached.