01 Knapsack using Memoization | Concept of Memoization

Sdílet
Vložit
  • čas přidán 6. 09. 2024
  • In this video, I have explained the basic concepts of memoization using 01 knapsack as an example.I have already explained the 01 knapsack using recursion method in my previous video and here I have continued the topic and showed how to apply smart recursion using an additional data structure.Remembering previously calculated subproblem values so as to not recalculate will improve time complexity and that's the goal of using memoization.We can remember the past by storing results in certain data structures which is generally array or map.I have first shown the example for why we need optimization on recursion and then I have shown how to optimize the recursion by using simple examples and code explanation.
    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 :)
    ========================================================================
    Join this channel to get access to perks:
    / @techdose4u
    INSTAGRAM : / surya.pratap.k
    SUPPORT OUR WORK: / techdose
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    TELEGRAM Channel LINK: t.me/codewithT...
    TELEGRAM Group LINK: t.me/joinchat/...
    ========================================================================
    USEFUL LINKS:
    01 Knapsack Recursion: • 01 Knapsack using Recu...
    01 Knapsack DP: • 01 Knapsack Tabulation...
    #01knapsack #dp #memoization

Komentáře • 59

  • @bestsaurabh
    @bestsaurabh Před 3 lety +14

    What you explained is the top-down approach having recursive calls. For iterative solution where we solve the smaller problem first and build on top of that is called bottom up approach.

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

      Yes you are right. In terms of subproblems Memoization is Top-down and Tabulation is bottom up. But I am used to talking in terms of directions in a table. If you fill top-down and left-right then it's actually bottom-up but due to direction, I am used to saying Top-Down.

    • @schan263
      @schan263 Před rokem

      @@techdose4u Thanks for clearing that up. I was a bit confused when you said top down at the end of the video.

  • @nagendrahegde
    @nagendrahegde Před 2 lety +6

    Hey, this video actually made me understand knapsack solution! I am preparing for this big ass interview where mostly they will ask DP problems. I was struggling with the knapsack problem and could not understand well from gfg (btw, the content in gfg is great, but it was something with me that i could not somehow feeling the intuition). But after watching this video, i could easily corelate. Thanks dude!

  • @vivek.tiwary
    @vivek.tiwary Před 3 lety +5

    In 2 d array, column should go till 10 that is the max capacity. Sometimes you are saying capacity 5, and you are calculating for 10, hell lot of confusion

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

    His voice is so satisfying.

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

    Sir, it's a great feeling to learn from you, one kind request though, please attach some practice problems related to the topics you teach in every video, I wanted to learn dp from a long time

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

      I will do problems after concepts. You can practice those.

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

      @@techdose4u Yeah you said that in your live stream, but if you can attach hand picked problem links, it'll be great help, either way you're great , keep up the good work

  • @tusharbarman1924
    @tusharbarman1924 Před rokem

    Better than all the other videos out there, thanks a lot man.

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

    Itni raat ko upload sone hi jaa rha tha par video dekhunga😂

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

    I think base case should be N < 0 instead of N == 0 since we have an zero indexed array .

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

    bhai result store karne ke liye mem[N][W] hoga na?
    ki mem[W][N] I am confused kyuki video main mem[W][N] hai but diagram(pichle slide) mein alag hai.
    Also ye array ka logic samaz nahi aa rha hai

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

      Yes you are right. I must have written by mistake.Thanks for letting everyone know.

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

      @@techdose4u Thanks for such a prompt reply. Ek doubt solve hogaya.
      another doubt is ki fibonacci example main hum ek hi array lete hai but isme 2-D array of no. of elements as row and weight as column. I am not getting it.
      kyuki maine jab dry run kia normal recursion wale code ka usme mujhe koi weight + num of element repeat hote hue nahi mila. I am confused because of that.

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

    *Those who are working on with Java, here's the code. Ty so much surya for helping me do it. capacity) {
    result =(getOptimalValue(capacity,values,weights,n-1,mem));
    }else {
    result = Math.max(getOptimalValue(capacity,values,weights,n-1,mem),
    getOptimalValue(capacity-weights[n-1],values,weights,n-1,mem)+values[n-1]);
    mem[n-1][capacity-1]= result;
    }
    }
    return result;
    }
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int capacity = sc.nextInt();
    int[] values = new int[n];
    int[] weights = new int[n];
    for (int i = 0; i < n; i++) {
    values[i] = sc.nextInt();
    weights[i] = sc.nextInt();
    }
    int mem[][] = new int[n][capacity];
    for(int i = 0;i

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

    Sir, thanks for the crystal clear explanation!!!

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

    Very nice and helpful video to understanding deeply of knapsack problem 🥰😊😍

  • @impatientgaming9868
    @impatientgaming9868 Před rokem

    good one.

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

    16:18, result would be set to memo array when all the recursion calls been done right? I mean the program would go to the block when the computation is been over, I'm pretty confused on how memorization works.
    Great video, understood like a charm. Thank you ❤️❤️

    • @techdose4u
      @techdose4u  Před 3 lety

      Yes right. Welcome :)

    • @dhanviakash726
      @dhanviakash726 Před 2 lety

      Yeah
      Actually it's not using the mem table that we created to lookup
      I put a counter on that lookup block
      if(mem[n-1][capacity-1] != -1){
      counter++;
      return mem[n-1][capacity-1];
      }
      My counter value is 0 after program has finished

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

    Great Explanation , can you explain manacher's algorithm its very confusing

  • @gajanoor
    @gajanoor Před 2 lety

    Thanks a lot for the video. Appreciate your help. Shouldn't the base check be N < 0 instead of N == 0? Assuming the N value passed into the function is zero-indexed?

  • @panmacabre9895
    @panmacabre9895 Před 2 lety

    your channel is the best

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

    Great video, but if you will explain recursion part little bit more precise then it will be very helpful to those who gets stuck with recursion.

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

      Recursion is in the previous video. Please comment there if you get any issues related to recursion.

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

    how can we know if our memoization solution will be getting stack overflow or not?

  • @ivanturko8881
    @ivanturko8881 Před rokem

    Priceless!

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

    Dear Sir, Thanks you vary much, Appreciate!!! you approach

  • @isacitrabuana8689
    @isacitrabuana8689 Před rokem

    thank you

  • @maddinenianilkumar8737

    Sir, how to store 0/1 in a solution array to indicate which item is included and which is not?

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

    excellent!!!!! Thank you so much

  • @abhishekjaiswal2575
    @abhishekjaiswal2575 Před 2 lety

    I have a doubt that can we travel from 0 to n and increase n+1 every time. I mean can we move from left to right in weight array.

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

    Thanks

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

    Superb

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

    thanks a lot bro u rock

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

    superb😀

  • @DEVANSHGOEL-dq1wh
    @DEVANSHGOEL-dq1wh Před 3 lety +1

    thank you sir

  • @Sandeep-lm5yi
    @Sandeep-lm5yi Před 3 lety

    For all those looking for practice problems
    Here you go:
    atcoder.jp/contests/dp/tasks

  • @HimanshuSingh-ob9qo
    @HimanshuSingh-ob9qo Před 3 lety +1

    👍

  • @mwshubham
    @mwshubham Před 3 lety

    16:15 mem[w][n] or mem[n][w] ?

    • @mwshubham
      @mwshubham Před 3 lety

      Btw amazing video learning this topic today.

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

      I think I wrote something wrong. Please check it 😅

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

      @@techdose4u yup no worries. Thanks for your reply.

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

      @@mwshubham 👍🏼