01 Knapsack using Memoization | Concept of Memoization

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ก.ย. 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

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

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

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

  • @nagendrahegde
    @nagendrahegde 2 ปีที่แล้ว +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!

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

    His voice is so satisfying.

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

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

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

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

  • @padmaraghunathan4849
    @padmaraghunathan4849 3 ปีที่แล้ว +1

    Sir, thanks for the crystal clear explanation!!!

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

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

  • @kunalsoni7681
    @kunalsoni7681 3 ปีที่แล้ว +1

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

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

    your channel is the best

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

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

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

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

    good one.

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

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

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

    Priceless!

  • @dhanashreegodase4445
    @dhanashreegodase4445 3 ปีที่แล้ว +1

    excellent!!!!! Thank you so much

  • @vijayj1997
    @vijayj1997 3 ปีที่แล้ว +1

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

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

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

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

      Yes right. Welcome :)

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

      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

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

    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?

  • @techmoon_
    @techmoon_ 3 ปีที่แล้ว +1

    thanks a lot bro u rock

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

    Superb

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

  • @syedkaja2045
    @syedkaja2045 3 ปีที่แล้ว +1

    superb😀

  • @sakshamsharma5484
    @sakshamsharma5484 10 หลายเดือนก่อน

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

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

    thank you

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

    Thanks

  • @DEVANSHGOEL-dq1wh
    @DEVANSHGOEL-dq1wh 3 ปีที่แล้ว +1

    thank you sir

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

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

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

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

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

    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.

  • @HimanshuSingh-ob9qo
    @HimanshuSingh-ob9qo 4 ปีที่แล้ว +1

    👍

  • @Sandeep-lm5yi
    @Sandeep-lm5yi 3 ปีที่แล้ว

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

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

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

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

      Btw amazing video learning this topic today.

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      I think I wrote something wrong. Please check it 😅

    • @mwshubham
      @mwshubham 3 ปีที่แล้ว +1

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

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว +1

      @@mwshubham 👍🏼