Lecture 110: 0/1 KnapSack Problem || learn 2-D DP Concept || DP Series

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 194

  • @shubhambarore5548
    @shubhambarore5548 2 роки тому +25

    Before watching this video, I was very confused about the dimension of DP , whether to take 2D / 3D , now I feel comfortable with it, I appreciate your efforts for this free and Top-Notch content !!!

  • @TheEleetcoder
    @TheEleetcoder 2 роки тому +8

    The demagorgan of dsa no one can kill!! You are amazing!!

  • @brain1212
    @brain1212 Рік тому +8

    The fear of dp is rapidly decreasing due to such great videos
    Thanks a lot and lot bhaiya
    Feeling lucky to have your videos for my preparation

  • @aniketkumar3218
    @aniketkumar3218 2 роки тому +32

    Questions:
    1. 0/1 Knapsack
    2. Equal Subset Sum Partition
    3. Subset Sum
    4. Minimum subset sum difference
    5. Count of subset sum
    6. Target Sum

  • @hardikshukla4508
    @hardikshukla4508 2 роки тому +8

    Never seen such simple and more descriptive way to understand such a complex problem, Such DP videos are just awesome :)

  • @front-dive
    @front-dive Рік тому

    bhaiiya jab aapne bola na left to right tb mere chehre pe ek smile thi or fir turant hi click kiya ki right to left se kuch nhi fatega. aapko sb pta hai kha kon bnda fas skta hai you are great teacher love you bhaiya 💖💖

  • @146_shashanks4
    @146_shashanks4 2 роки тому +12

    I love the way u teach bhaiyaa still I have not watched this vedio due to my exams which starts from tomorrow but I can undoubtedly say it will be in next level I just came here to comment pls don't stop this dp series bhaiyaa I am literally depending on you once my exam completes I will resume watching your vedios bhaiyaa
    Lots of love from my side
    I am from Bangalore Karnataka

    • @peterpace3379
      @peterpace3379 2 роки тому +13

      Address bhi de dete apna

    • @ytg6663
      @ytg6663 6 місяців тому

      Stop this bhaiya culture

  • @sal2054
    @sal2054 2 роки тому +12

    Happy Teacher's Day Bhaiya.Grateful for everything you have done for us.May Krishna bless you profusely.🙏🙏

  • @sandeeptottadi
    @sandeeptottadi 2 роки тому +9

    Just needed DP. Thank you 💕

  • @jaysoni4180
    @jaysoni4180 Рік тому +2

    consistency ++ ! and yes consistency is Underated!

  • @shreyashdubey5989
    @shreyashdubey5989 2 роки тому +4

    ❤❤ DP looks simpler now , Lecture Completed... Keep going bhaiya👏👏

  • @govindsuryavanshi6653
    @govindsuryavanshi6653 Рік тому +1

    First I have completed all DSA series and now conclude this is Greatest ever DSA series to exists on youtube or paid courses. Your contribution will be remembered. You're God of DSA for us🙇‍♂ Thanks you.

    • @therealartist9
      @therealartist9 9 місяців тому +2

      bhai tu bada khali aadmi hai yar

  • @DPCODE72
    @DPCODE72 2 роки тому +64

    Ohhhhhhh bhaiyyyyaaa, one thing that I would like to tell you that I'm not sure that I will place in Product based company whether yes or not. But honestly I have been following your each & every video on UA-cam. So it will not matter that I will be placed or not but I'm sure that end of the knowledge I will make a character that will inspire to many students in upcoming time. Great thank you bhaiya!!. Just feel narvou or depressed 😔 nowadays!!

  • @jayant2366
    @jayant2366 2 роки тому +1

    bina bahut jada coding jane bhi agar aapki video se pahdo to bhayia pura pura samjh ata h.

  • @TheEleetcoder
    @TheEleetcoder 2 роки тому +8

    Thank You bhaiya !! I am solving all the questions you teach in your lecture and explain it my way and then upload the video on youtube! i want to try the teaching field as well. I even mailed you regarding your team (to be a part of your team.....hope you read it!)

  • @muzammilaltafmulla
    @muzammilaltafmulla 3 місяці тому +2

    This is by far the best Knapsack video I've ever seen...

  • @ayushsoni3681
    @ayushsoni3681 4 місяці тому

    Last approach dekh kar toh maza hi aa gya bhaiya , thanks a lot !!!

  • @asifmondal7609
    @asifmondal7609 2 роки тому +2

    At first... Thank You Very Much bhaiya.. cum sir! :)
    I don't know that if this much adventure was already present in this question how much I felt... But in this whole solution process by using all methods.... I enjoyed very much and learned a lot. During the whole hour... in every moment every point was clearly realized by your logic and describing. Thank you so much again sir

  • @chinmaypanchal2521
    @chinmaypanchal2521 Рік тому +10

    w = weight[0] kyu lagaya usi pe toh atka hun 28:40

    • @UtkarshSinha-ok5fg
      @UtkarshSinha-ok5fg 6 місяців тому +1

      It actually doesn't matter if you start it with w=0 or w=weight[0], if you look at next if condition then....all values from w=0 till w

    • @kushaalrana
      @kushaalrana 4 місяці тому

      @@UtkarshSinha-ok5fg one more question why did 2d dp have n, w+1, why not n+1 and w+1

    • @UtkarshSinha-ok5fg
      @UtkarshSinha-ok5fg 4 місяці тому

      @@kushaalrana because for including W in the array we would need indexes from 0 to W and hence the required size of the array would be W+1,
      for n we don't have to use n+1 because the robber is carrying n items so an array of size n would do the job.

  • @Sahil1001
    @Sahil1001 2 роки тому

    sir ji late cahl raha hu dheere dheere pahuchunga isme !! just bata raha hu ki aapka kaam best h !!!

  • @PoliticalTruth1
    @PoliticalTruth1 2 роки тому +1

    Bhaiya apse padh ke bahut hi achha laga dil se apko thanks bolna chahta hu Q ki ap ke teaching style ka to hm fan ho gye hai
    Bhaiya plz is course ko total complete kijiye ga bahut ummid hai is course se mere future ke liye
    Once again thank you bhaiya ❤️

  • @believer-n3t
    @believer-n3t 3 місяці тому

    highly recommended video for knapsack. best video i have ever seen, can be further more better if you show the optimization by putting the value in dp.
    but i understand its time issue. ❤

  • @harshalpatil4496
    @harshalpatil4496 Рік тому

    Great explanation. Glad that you covered all the scenarios which gives crystal clear clarity for the given problem statement.

  • @golubhattuk01
    @golubhattuk01 7 місяців тому

    i didn't understatand the last 2 videos but this one amazing cleared all the doubts

  • @pramantomar822
    @pramantomar822 2 роки тому +2

    Loving the series ❤️❤️❤️❤️❤️❤️❤️❤️

  • @prajaktabhalerao1239
    @prajaktabhalerao1239 Рік тому +4

    Bhaiya, why have you returned prev[capacity] and why not curr[capacity]? n-1 denotes the last row right, which is curr.

  • @anshumanacharya9812
    @anshumanacharya9812 Рік тому +2

    Thank you so much bhaiya...
    But, how can somebody think of starting the loop from w = weight[0] to maxWeight in analysing the first base case?
    Please somebody reply if have any logic from the front, or how it would click if asked impromptu?..

  • @beingoutofthebox8906
    @beingoutofthebox8906 2 роки тому +2

    Thanku
    Explained the concept in a very easy way.

  • @Eng...1
    @Eng...1 10 місяців тому +1

    Just amazing lecture 🎉😊

  • @shubham_jha
    @shubham_jha Рік тому +1

    Babbar bhaiya!!

  • @rachit_joshi
    @rachit_joshi 4 місяці тому

    Thank You So Much BHRATA SHREE !!!!!!!

  • @zeroooo___
    @zeroooo___ Рік тому +4

    I am having problem where you write the code of =>analyse the base case in solveTab( )
    Why you run for loop from weight [0] to capacity

    • @souvikmondal04
      @souvikmondal04 4 місяці тому

      You can write like below also -
      //for all index 0 and weight from 1 to max weight we need to take care
      for(int i = 1; i

  • @lakshyabaliyan7917
    @lakshyabaliyan7917 2 роки тому +6

    Bhaiya c++ DSA me leetcode par qus solve nhi ho rhe hai 😭

    • @DPCODE72
      @DPCODE72 2 роки тому +6

      Don't worry, just keep trying & practicing!!

    • @technoakshay20
      @technoakshay20 Рік тому

      Ab ho raha hai kya ?

    • @lofireverbz-wy7go
      @lofireverbz-wy7go Рік тому +1

      @@technoakshay20 ab uski placement lg gyi bhai

  • @agnivashil4130
    @agnivashil4130 Рік тому +3

    It would have been better if how the 2D array got filled had been shown; otherwise, great explanation!

  • @Kushal-p7i
    @Kushal-p7i 11 місяців тому

    Nice, thnks for teaching different approaches ❤

  • @shalinisaini3787
    @shalinisaini3787 25 днів тому

    Thank You for this amazing lecture

  • @vaishnavrushikesh4022
    @vaishnavrushikesh4022 2 роки тому

    Bhai Gajab ka explain Kiya hai. Maja aa gaya.

    • @piyushgaur6975
      @piyushgaur6975 2 роки тому

      bhai mujhe smjh nhi aaya yaar Bottom Aproach mein DP intialization smjh
      for(int j = 1; j

    • @vaishnavrushikesh4022
      @vaishnavrushikesh4022 2 роки тому

      @@piyushgaur6975in initialisation, it is not weight[0]. It must be value[0].
      J= 1 to W means we are checking weight from 1 to W. dp[0][w] show that when we have one element than what kind of profit we get. If weight of 1st element is less than bag weight than we have to take first element so that time we get the profit of first first element.
      Ex : bag cap = 10kg
      1st element weight = 5kg
      Than
      dp[0][1] to dp[0][4] is zero
      And dp[0][5] to dp[0][6] is value[0].

    • @piyushgaur6975
      @piyushgaur6975 2 роки тому

      @@vaishnavrushikesh4022 arey bhai thank you very much bhaii, mein bta nhi skta kitna pareshaan hogya tha m ...smjh hi nhi aara tha thanks

    • @piyushgaur6975
      @piyushgaur6975 2 роки тому

      One thing I have to say that, In the initialization loop of bottom up approach
      for(int j = weight[0]; j

    • @vaishnavrushikesh4022
      @vaishnavrushikesh4022 2 роки тому +1

      @@piyushgaur6975 correct but note one thing when you make dp vector thay you have to initialise all the elements to 0.

  • @iteself_me
    @iteself_me Рік тому

    138 ✅completed 👍Liked 12:07

  • @sachinrathod8319
    @sachinrathod8319 2 роки тому

    last solution was fab💥

  • @subhadeepchaki9529
    @subhadeepchaki9529 2 роки тому +4

    In first loop why it start with w=weight[0] not w=0, i guess weight array is not given in sorted order. I tried using w=0 it's giving wrong answer. Can anyone explain please ??

    • @shashwatkumar6965
      @shashwatkumar6965 2 роки тому +2

      no, bro. you can actually start with w = 0. only change is instead of checking if weight[0]

    • @kushaalrana
      @kushaalrana 4 місяці тому

      @@shashwatkumar6965 can you also clarify why is dp array starting from n,capcity+1 and why not from n+1, capacity+1

    • @shashwatkumar6965
      @shashwatkumar6965 4 місяці тому +1

      @@kushaalrana For defining the size of dp array always look at how you are calling the solve recursive function for the first time from your main code. Since solve is called with solve(n - 1, maxCapacity) therefore we need to have size n and capacity + 1 to store all the results. It's no harm in creating a dp array with n+1 as well, but the nth position will never be used since the all the elements are from index 0 to n-1.

    • @kushaalrana
      @kushaalrana 4 місяці тому

      @@shashwatkumar6965 Thank you so much❤️, that was very precise 🔥💯

  • @khushboobuwade2932
    @khushboobuwade2932 2 роки тому +1

    Bhaiya poori playlist kab tk bnegi ...ye Last video hai ya aur aage kitne months mai complete hoga course

  • @sanketharvande8773
    @sanketharvande8773 2 роки тому +4

    Bhai pls make programs in java also.......💯pls bhai, it'll be helpful for more JAVA people including me 🙂

  • @VivekKumar-f7b3g
    @VivekKumar-f7b3g Рік тому

    Bhaiya last wala line mast the left to right😂😂

  • @ND-bm6im
    @ND-bm6im Рік тому

    you are such a great great bhaiya we all are love you bhaiya so much

  • @aniruddh2175
    @aniruddh2175 2 роки тому +3

    Why do we initialize dp array with -1 while top-down approach (Memoization) and with 0 while bottom-up (tabulation)?

    • @vatsalshingala3225
      @vatsalshingala3225 Рік тому

      you can do anything

    • @usernamewhatsoever4557
      @usernamewhatsoever4557 Рік тому

      because -1 will never be an answer that will be computed for a subproblem
      so if array has -1 stored means this subproblems answer hasnt beeen computed and stored in array yet

    • @usernamewhatsoever4557
      @usernamewhatsoever4557 Рік тому

      meanwhile in bottom up, vectors automatically initialize with 0 so you dont need to explicitly initialize with 0

  • @rashidhridoy2017
    @rashidhridoy2017 Рік тому +3

    How can I pay my gurudakshina?

  • @samirkhandekar4678
    @samirkhandekar4678 5 місяців тому

    Thank you. But did you guys not find it overwhelming? So many concepts to grasp.

  • @therealartist9
    @therealartist9 9 місяців тому

    some concepts which is not explained -> hameny dp initialization mai maxwieght kyu liya jabki ham index lete the , 2nd ->value aur capacity dono ki value change maar ri hia

  • @SajanKumar-ec2us
    @SajanKumar-ec2us Рік тому +1

    Explain how to write for loop in base cases

  • @saadshakeel3348
    @saadshakeel3348 4 місяці тому

    Is there any benefit of memoization in knapsack problem as there is no overlapping sub problem in dry run.

  • @Geetanjalichawla
    @Geetanjalichawla 2 роки тому +2

    🔥🔥first comment

  • @jatinlodhi980
    @jatinlodhi980 Рік тому +1

    bhiya prev = curr and curr = new int[capacity+1]

  • @abhinavsinghal314
    @abhinavsinghal314 2 роки тому +1

    Bro for greedy, graphs etc also we need roadmap similar to dp

  • @RashmiBajaj-mf3el
    @RashmiBajaj-mf3el Рік тому +3

    Somebody please clear my doubt here!!
    In the solveTab method, in base case analysis
    why is the loop going from wt[0] to capacity ? Why not from 0 to capacity?
    Please help me understand this, anybody..

    • @RashmiBajaj-mf3el
      @RashmiBajaj-mf3el Рік тому

      @CodeHelp - by Babbar

    • @kostubh
      @kostubh Рік тому

      So here is the explanation-
      Here dp[i][j] will give you the value of the ith index with j weight, so now for the base case we are considering the 0th index only, which will have the wt[0] initially.
      Aise socho, hum uska weight increase kar rahe hai, aur hum tab tak increase kar sakte hai, jab tak vo capacity se jyada na ho jaaye.

    • @abhinavarya7752
      @abhinavarya7752 Рік тому +1

      Kya ho if weight[0] is greater than capacity?

  • @keshavarya5999
    @keshavarya5999 2 роки тому

    Bhayankar 🎉🔥

  • @acxd
    @acxd 2 роки тому

    charging karte hue kya camera record nahi karta kya , ya in camera's ki battery alaga se nikal ke change hoti hai??

  • @VishalKumar-ez2vj
    @VishalKumar-ez2vj 2 роки тому

    dil garden garden ho gya ! 😃 Thanks alot bhaiya !

  • @shachisinghal8856
    @shachisinghal8856 4 місяці тому

    sir, in some memo Q you start index from 0 and base case at n-1, in others from n-1 and base case at 0. Why this confusion? Can someone clear!!

  • @md_arfin_cse
    @md_arfin_cse 2 роки тому

    Superb bro
    Best Best DSA course on UA-cam.

  • @swapnilgt
    @swapnilgt 7 місяців тому

    In the tabular way, when analysing the base case (step 2), not sure why have we used "int w = weight[0]". Since we want to iterate from weight = 0 to weigth

    • @NNUK28
      @NNUK28 6 місяців тому

      in this case rows[0] all cols value will set as 0 means this weight can't fit on this bag

  • @siddharthambast7745
    @siddharthambast7745 2 роки тому

    Brilliant Content

  • @arzitmahajan7703
    @arzitmahajan7703 2 роки тому +2

    Can we implement recursive approach left to right and make the base case if(index ==n-1){....}. If not can anyone please explain why?

    • @ramanjangra7465
      @ramanjangra7465 11 місяців тому

      YES we can do in that way also

    • @ramanjangra7465
      @ramanjangra7465 11 місяців тому +1

      gfg ans :
      int solve(int w, int i, int wt[], int val[],int n, vector&dp){
      if(i==n-1){
      if(wt[i]

  • @KhushiKavya-py1yj
    @KhushiKavya-py1yj 4 місяці тому

    consistency++

  • @Avez5
    @Avez5 2 роки тому +1

    Present Bhaiya

  • @raorao7329
    @raorao7329 6 місяців тому +2

    Jabse dp aur graph shuru hue hai tabse samajh aana kam ho gya bhaiya

  • @mdmasleuddin4992
    @mdmasleuddin4992 2 роки тому +1

    Sir plz 40 lecture ke baad ka note upload kar dijiye

  • @namanpadiyar09
    @namanpadiyar09 6 місяців тому

    tough lagra bhaiya🥲

  • @yashraghuwanshi7710
    @yashraghuwanshi7710 7 місяців тому

    sir yeh lecture ka explanation bilkul sahi nhi lga vector m n(row hai kaise ptaa) or maxweight(column hai yeh kaise ptaa chla) maxweight+1 he kyun kiya or tabulation for lopp condition base kaise hua or achese nhi smjhaya sir please explain to me

  • @vishalgarna4580
    @vishalgarna4580 Рік тому +1

    Thoda samaj aaya 😅😅😅

  • @ayushdube-e9s
    @ayushdube-e9s 5 місяців тому

    atleast one dry run would help everyone to grasp the knowledge quickly.

  • @arvindmali5179
    @arvindmali5179 Рік тому +1

    best

  • @9852963775aaa
    @9852963775aaa 2 роки тому +1

    Bhaiya ab thoda confidence aa raha hai shayad kahi ache jagah lag jau.Thankyou bhaiya for this amazing content.

    • @piyushgaur6975
      @piyushgaur6975 2 роки тому

      bhai mujhe smjh nhi aaya yaar Bottom Aproach mein DP intialization smjh
      for(int j = 1; j

  • @theoneyt999
    @theoneyt999 2 роки тому

    ❤️🤞 thank you bhaiya

  • @taneyasoni
    @taneyasoni Рік тому

    why are we initializing include=0 and exclude=0 everytimme we call the function.

  • @vishalwaghmare1381
    @vishalwaghmare1381 2 роки тому +1

    Bottom up approach bohot fast hogayi, smj hi nahi aaya kya ho rha h :(, if anyone could make me understand what's happening, it would be helpful

    • @vishalwaghmare1381
      @vishalwaghmare1381 Рік тому

      Took time but understood by self exploring various solutions

  • @kingshukpal301
    @kingshukpal301 Рік тому

    Good content but lack of explanation why what happened especially tabulation

  • @ishansaxena6631
    @ishansaxena6631 2 роки тому +1

    Last wala optimization bilkul samajh nahi aya

  • @lucario4399
    @lucario4399 2 роки тому +1

    7:50 brute force

  • @thatindianboy2817
    @thatindianboy2817 2 роки тому

    Bhai ye jo tum call back kr rhe the (recursion- 2d)tabulation mein samjhane ki bjae, uska link to at least daal dia kro. Kewal code dekhna hota to idhr kyu aata bro

  • @vivan1852
    @vivan1852 2 роки тому

    Thank you bhaiya , attendance done

  • @JohnSmith-uu5gp
    @JohnSmith-uu5gp Рік тому +1

    Just Awesome!!!

  • @sukhjitsingh959
    @sukhjitsingh959 2 роки тому

    Thanks for vedio sukhjit here

  • @manavrollno.2314
    @manavrollno.2314 2 роки тому +1

    Now the video length is op

  • @harikrushnasuhagiya3925
    @harikrushnasuhagiya3925 2 роки тому

    Hello Bhaiya,
    I want to purchase your Comprehensive Data structure and algorithms course on unacedemy what should I do? Because in your course on unacedemy showing Enrollment close . I want to purchase your course please bhaiya ho ske to Enrollment on karvado na...

  • @unboxtheuniverse5336
    @unboxtheuniverse5336 2 роки тому +1

    Bhaiya abbb Continue Videos aayegi na 😃😃🔥

  • @rishabgupta2733
    @rishabgupta2733 Рік тому

    Can anyone just tell me why we have initialized dp array with 0 in tabulation method ?

  • @prashantpandey6078
    @prashantpandey6078 2 роки тому

    Great timing Love Bhaiya... Thanks

  • @RandomGuy-xq1hk
    @RandomGuy-xq1hk 11 місяців тому

    Nice video 😇😇😇

  • @SouravYadav-pb3xx
    @SouravYadav-pb3xx 2 роки тому

    Maja aa raha hai

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp 2 роки тому

    Yeahhh 🔥🔥🔥.... Do you need any Web Development Intern... I will be really glad

  • @sidver2578
    @sidver2578 Рік тому

    can someone explain why does he not get negative index error

    • @priyanshubansal4534
      @priyanshubansal4534 10 місяців тому

      becoz 0 doesn't have any effect on final answer but -1 will affect answer

  • @sauravraj2439
    @sauravraj2439 2 роки тому

    Bhaiya mujhe only java language aati h , cpp ni aati.... Dp series kaise start kru ?

  • @MonkeyDLuffy-t2b
    @MonkeyDLuffy-t2b Місяць тому

    21:29 32:39

  • @B-NikhilRichhariya
    @B-NikhilRichhariya 11 місяців тому

    hash last smjhaa

  • @harishkumar2107
    @harishkumar2107 2 роки тому

    Love babbar sir you are best 🔥🔥❣️

  • @Kratikgarg
    @Kratikgarg 2 роки тому

    bhaiya aapne jaise notes suru main dale the vese notes aapne sari videos ke bna ke kyun nhi daale please bhiya daal do isse hm log ko bahut help mlegi

  • @NotTooHard8209
    @NotTooHard8209 3 місяці тому

    anothe rway to solve this easily 😉
    static int solve(int w, int wt[], int val[], int i){
    int sum = 0, max = 0;
    for(int j=i;j

  • @appsuniverse9976
    @appsuniverse9976 2 роки тому

    very good explanation about knapsack problem

  • @subhamdas943
    @subhamdas943 Рік тому +1

    ✔✔✔✔

  • @sachinsagar4718
    @sachinsagar4718 2 роки тому +2

    why we have initialized w by weight[0]..??? Can anyone explain