DP 23. Unbounded Knapsack | 1-D Array Space Optimised Approach

Поделиться
HTML-код
  • Опубликовано: 20 окт 2024
  • Lecture Notes/C++/Java Codes: takeuforward.o...
    Problem Link: bit.ly/3IvPdXS
    Pre-req for this Series: • Re 1. Introduction to ...
    a
    Make sure to join our telegram group for discussions: linktr.ee/take...
    Full Playlist: • Striver's Dynamic Prog...
    In this video, we solve the problem of minimum coins. We start with memoization, then tabulation, then two-row space optimization. This problem is the seventh problem on DP on Subsequences Pattern. Please watch DP 14 before watching this.
    If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.o...
    You can also get in touch with me at my social handles: linktr.ee/take...

Комментарии • 489

  • @sandeeperukulla498
    @sandeeperukulla498 2 года назад +213

    Hey Striver, I solved the problem in all 3 approaches without watching the video. All credit goes to you, Thanks a lot. You are the best.

  • @tasneemayham974
    @tasneemayham974 Год назад +19

    STRIVER! I solved the whole problem in recursion, memoization, tabulation, Space optimization and then even the 1D Array ALL ALONEEE!!
    Thank youuuu! YOU ARE THE BEST TEACHER!!
    🙌👏👏

  • @GauravThinks
    @GauravThinks 8 месяцев назад +4

    For someone finding Recursive + memoised, tabulated, space optimised approaches:
    My flow of solving the dp is opposite to that of what Raj bhai do, (I prefer starting index from 0---> n-1) so accordingly, down below are my approaches:
    #include
    int helper(int ind, int capacity,vector &profit, vector &weight, int n, vector &memo){
    if(ind>=n) return -1e9;
    if(capacity==0) return 0;
    if(memo[ind][capacity]!=-1) return memo[ind][capacity];
    // take, nottake
    int take=0, nottake=0;
    if(weight[ind]

  • @jagannathan1014
    @jagannathan1014 Год назад +11

    best thing about him is , rather than teaching how to simply solve ,he teaches us how to think , which makes us solve on our own ! GOD level!

  • @_Kunal_Pawar
    @_Kunal_Pawar 8 месяцев назад +1

    Understood! Since you've already explained these concepts so well in the previous problem, I was able to solve it on my own. Thanks, Striver! 💯

  • @sarthakragwan5248
    @sarthakragwan5248 2 года назад +15

    Even befor you explained the question just by name of topic i got the que what we gona do and i smiled like just 10 days ago i was suffering from DP and now i can predict what the que gonna be 😌Thanks Stiver Respect +++✌️

  • @mantrarajgotecha3055
    @mantrarajgotecha3055 2 года назад +10

    Whhoooaa striver, excellent concept.
    Without seeing the solution, I am able to solve it with all approaches, all credit goes to you sirji.

  • @nilimsankarbora5911
    @nilimsankarbora5911 9 месяцев назад +1

    Hey Striver, I solved the problem in all 3 approaches without watching the video. All credit goes to you, Thanks a lot. You are the best.❤❤❤❤❤

  • @SelvaArumugam-xq5mf
    @SelvaArumugam-xq5mf 9 месяцев назад

    Hey Striver, I solved the problem in all 3 approaches without watching the video. All credit goes to you, Thanks a lot. You are the best.

  • @meetharsoda5152
    @meetharsoda5152 4 месяца назад +6

    1-D Array Approach was amazing. Thank You

    • @lakshsinghania
      @lakshsinghania 3 месяца назад

      why didnt we do from right to left ? the iteration

    • @SujalMahalaha
      @SujalMahalaha 3 месяца назад +1

      @@lakshsinghania Because we need the current guy’s backward data and not previous guy’s

  • @thepleasantmoment2705
    @thepleasantmoment2705 9 месяцев назад +1

    Hey Striver, I solved the problem without watching the video. All credit goes to you, Thank you so much.

  • @ankishkhandelwal1061
    @ankishkhandelwal1061 2 года назад +1

    i solved this problem in all approach recursion >>>>> memo>>>> tabulation >>>>> space optimization[ 2 * 1d array] >>>>>> space optimization[1d array]
    without watching the video..
    thank you so much ...
    you are amazing teacher ..

  • @adityarai30
    @adityarai30 Год назад +3

    Hey striver, I have been watching your videos since very long and especially this playlist has helped me overcome the fear of dp.....thanks a lot🙏🙏♥♥

  • @imranimmu4714
    @imranimmu4714 Год назад

    Solved the problem without watching the video.
    Thank you for the knowledge you are providing.

  • @dipanshusharma9084
    @dipanshusharma9084 2 года назад +2

    understood, done the problem from recursion to tabulation with space optimization by myself..... very thankful for this dp playlist.

  • @KeigoEdits
    @KeigoEdits Месяц назад +2

    If someone is wondering why we iterate in forward manner in inner loop in unbounded knapsack while 1-d array optimization, and in reverse order in bounded knapsack, just remember this rule of thumb
    For bounded knapsack problems (each item can only be used once), reverse iteration is important to avoid reusing the same item.
    For unbounded knapsack problems (items can be reused), forward iteration ensures you're considering the possibility of reusing the same item multiple times within the same loop

  • @Sachin-je7ue
    @Sachin-je7ue 9 месяцев назад

    I followed this series from first videos and at this stage.I did this question in 11:44 min without watching this video. Thanks bro for your efforts to make such playlist.

  • @himanshuagrawal8012
    @himanshuagrawal8012 2 года назад +7

    I Solved By my own till 2 array space optimization,..Thanks a lot for the awesome quality content #UNDERSTOOD

  • @amitkulkarni9682
    @amitkulkarni9682 5 месяцев назад

    Only RUclipsr who will teach you in way so that you think on your own and solve it without watching his video. No Words !💯

  • @suhaanbhandary4009
    @suhaanbhandary4009 Год назад

    From not solving to any problem using Tabulation to Solving at first go, thanks for In-depth DP video!!

  • @SYCOA12CHAITANYAASOLE
    @SYCOA12CHAITANYAASOLE 2 месяца назад

    Understood !! hey striver i solved the problem on my own, feels amazing, keep going , keep growing !!

  • @DevashishJose
    @DevashishJose 9 месяцев назад

    thank you so much striver, my understanding has improved so much watching these video. may god bless you.

  • @KUMARSAURABH-s5i
    @KUMARSAURABH-s5i 2 месяца назад

    Thanks, solved using all the 3 approach without watching this video.
    Great dp series

  • @shivammaurya9630
    @shivammaurya9630 10 месяцев назад +2

    You actually made dp easy now I only need to think of recursion and that's it🌝

  • @EliasEH89
    @EliasEH89 8 месяцев назад

    Best DP series in all RUclips.

  • @surajmisal3898
    @surajmisal3898 2 года назад

    Following this series from first video and Solved this question without watching the video. (I would not have solved any problem in last month same date but now I can solve basic and medium level dp problem). Thanks to striver.

  • @socify4410
    @socify4410 Год назад

    i done it my own although it was based on preveous problems but this shows you are getting ready for new preblems too.

  • @cosmiccometyt
    @cosmiccometyt Месяц назад

    Thank you so much striver, i solved the problem using all 3 approaches without watching the video. I didn't think that i had it in me to do it but hey, i did it. 😭😭

  • @rushirajparekh9079
    @rushirajparekh9079 5 месяцев назад

    Superb.. last level of space optimization!!

  • @ShubhamKumar-fn5be
    @ShubhamKumar-fn5be Год назад

    understood but i did this before watching the video to test whether i am able to solve. You have made us think on our own Thanks Striver.😄😄

  • @shaktiprasannasahoo7845
    @shaktiprasannasahoo7845 2 года назад +8

    Understood😃 and please make some videos where we can't use 2D Vector ,i.e. we need hashmaps to store them becoz I'm facing problem on that kind of qs.
    Your contents are the best👌👌.
    Thank you

  • @sachinchoudhary4142
    @sachinchoudhary4142 6 месяцев назад +3

    I am not understanding why we are not traversing for capacity from back for 1D space optimization as for cur index we need prev index values and cur array prev values so why not from back traverse

  • @Sara-ed3jq
    @Sara-ed3jq Год назад

    I got till memoization but made small mistake in tabulation, thankyou for teaching so well !

  • @somnathroy102
    @somnathroy102 Год назад

    I was able to do it and do no believe it myself. thank you so much.

  • @omkarkawatgi
    @omkarkawatgi 2 года назад

    Base case i

  • @koushande5679
    @koushande5679 2 года назад

    Solved before watching the video.. you have made dp easy for me :) Thank you

  • @mriduljain6809
    @mriduljain6809 2 года назад

    Was able to solve all 4 approaches without seeing the video, although was very delighted to watch the space optimization.
    Thank you bhaiya for making dp so easy:))
    "Understood"

  • @aliabbas-kj7lm
    @aliabbas-kj7lm Год назад

    was able to solve at one go.
    Well done Striver

  • @anshsaxena7297
    @anshsaxena7297 2 месяца назад

    Thanks alot striver, I have completed the problem on my own. Thanks alot!

  • @RohitKumar-dy2gc
    @RohitKumar-dy2gc 11 месяцев назад

    1d array approach is super wonderful 😊😊😊😊

  • @shubh625
    @shubh625 2 месяца назад

    understood and did till space optimization on my own
    missed

  • @DivineVision201
    @DivineVision201 2 года назад +1

    bhaiya content bohot acha hai apka. i am really enjoying it. thanks for this

  • @shivkumar-og4ow
    @shivkumar-og4ow Год назад

    understood bhaiya! i solved this problem all three approch without watching this vedio.

  • @manishprajapati8544
    @manishprajapati8544 2 года назад

    hello striver solved this question in all approaches except the tabulation one got confused of base case but i was close to exactly what you have done in this video. that means i'm learning great thanks for this wonderfull playlist.

    • @sagartaak4017
      @sagartaak4017 Год назад

      i get you bro u must not start your base case for loop from weight [0] to w+1
      instead u start it from 0 to w+1;
      same happen here!!

  • @ritikshandilya7075
    @ritikshandilya7075 4 месяца назад

    Thankyou for great explanation striver

  • @MukeshKumar-cc3uh
    @MukeshKumar-cc3uh 7 месяцев назад

    Awesome content in this series! Thank you and Understood ♥

  • @lohitaksha244
    @lohitaksha244 2 года назад +1

    Understood, did all and even space optimization to 1 1d array before watching video

  • @morganyu3391
    @morganyu3391 2 года назад +3

    This was the video which solidified my understanding in DP from all the concepts learnt in the previous videos. thank you so much Striver Bhaiya! Understood not only this video but the previous videos clearly!

  • @dennyage4791
    @dennyage4791 2 года назад +2

    I done it by my self , all thanks goes to striver 😁😁

  • @ratinderpalsingh5909
    @ratinderpalsingh5909 2 года назад +2

    Understood, sir. Thank you very much.

  • @chitranshjain5075
    @chitranshjain5075 2 года назад +2

    Understood. Kudos to you for the amazing job! Can you make a playlist on Array problems too?

  • @ranasauravsingh
    @ranasauravsingh 2 года назад +2

    UNDERSTOOD... !
    Thanks striver for the video... :)

  • @shreyasnagabhushan4918
    @shreyasnagabhushan4918 2 года назад

    understood , amazing explanation, sets clearly in the mind

  • @princeshubham26
    @princeshubham26 Год назад +1

    Solved by myself all memoization, tabulation and space optimization done.😀😀

  • @prabhakaran5542
    @prabhakaran5542 7 месяцев назад +1

    Understood ❤

  • @cypher7536
    @cypher7536 2 года назад

    Bhaiya recursion to space optimisation khud likha pura ❤️❤️❤️

  • @ketansarode17
    @ketansarode17 2 года назад +1

    striver....u are like a gem for us🥺☺

  • @sumerrawat6947
    @sumerrawat6947 Год назад +1

    Here why are we using overidden values from the left ? unlike in knapsack problem where we went from the right

  • @cinime
    @cinime 2 года назад

    Understood!! Thank you very very VERY MUCH!!!

  • @udatyadeb101
    @udatyadeb101 Год назад

    understood, but I solved this problem myself without seeing the solution upto 2 array space optimization. Needed the video to realize further optimization to a single array.

  • @arvindjaiswal8013
    @arvindjaiswal8013 4 месяца назад

    Awesome explanation...

  • @kathanvakharia
    @kathanvakharia Год назад

    Understood...Completed (23/56)

  • @Enigm.1.
    @Enigm.1. Месяц назад +1

    understood

  • @shubhamkumar-hx1fb
    @shubhamkumar-hx1fb 8 месяцев назад

    us...and also submitted the question in all the approaches by myself

  • @UtkarshWasHereBeforeYou
    @UtkarshWasHereBeforeYou 28 дней назад +1

    3 days ago: DP is one of my weekest topics, i fear it to death.
    DP-1: OK, LET'S DO THIS.
    DP-15-17 I think I'm getting a hang of it.
    DP-20s Can DO IT MYSELF NOW..
    bye, i'm going to understand all the remaining patterns ahead.
    and also, Understood.

  • @jeelanibasha3984
    @jeelanibasha3984 2 месяца назад

    Instead of pick to be zero cant we keep int_ min or -1e9 as we were finding the max element

  • @rishabhgupta9846
    @rishabhgupta9846 Год назад

    Understood,able to solve by myself till 2 array space optimization

  • @saibingi6980
    @saibingi6980 Год назад

    Hey, Striver
    I solved it on my own, So It is working for this base case also. Given below
    why it is working in this case
    if ind == 0:
    if W % weights[ind] == 0:
    return (W // weights[ind]) * values[ind]
    return 0
    instead of
    if ind == 0:
    return (W // weights[ind]) * values[ind]
    Why it is like that?

  • @abhirupbasu3386
    @abhirupbasu3386 2 года назад

    the 1-D array part was so good.
    UNderstood.

  • @KaranVerma-pf5jw
    @KaranVerma-pf5jw 11 месяцев назад

    Striver I think Recursion Worst Space Complexity without the DP array will be O(W + IND) , consider our n-1th index value is 1 and its wt is also 1 and our bag weight is 10 then by logic of standing at the same index first we will go till O(W) stack space and then all the non takes will kick in and we will go till ind == 0 which gives us O(IND) so space complexity will be O(W+IND)

  • @ganeshkamath89
    @ganeshkamath89 2 года назад

    Thanks Striver. Understood. Learnt a lot.

  • @sampreethmiriyala1086
    @sampreethmiriyala1086 2 года назад

    UNDERSTOOD....
    Thank you striver

  • @komalgoel8308
    @komalgoel8308 4 месяца назад +1

    understood☺

  • @iamnoob7593
    @iamnoob7593 9 месяцев назад

    understood , Thanks striver

  • @mayankbharti531
    @mayankbharti531 Год назад +3

    Can anyone explain why left to right (W=0 to maxWeight) does not work in 01 knapsack, but it does work in unbounded knapsack. I am very confused they look similar to me. Please help!

  • @johnsimon8158
    @johnsimon8158 4 дня назад

    thanks for this awesome content

  • @sujitmourya8481
    @sujitmourya8481 10 месяцев назад

    understood very well

  • @parthsalat
    @parthsalat 2 года назад +1

    Continue from 18:00

  • @pranaypatadiya
    @pranaypatadiya 2 года назад

    Smartest thing on the planet ❣

  • @TanviPoddar-f1k
    @TanviPoddar-f1k 5 месяцев назад

    Shouldn't we traverse the prev array from right to left as the current value is dependent on previous values which keep changing if we traverse from left to right in the single array approach?? You did that in the 0-1 knapsack problem.

  • @chanchalroy3417
    @chanchalroy3417 9 месяцев назад

    Understood! Just wow 😲

  • @nikhilbabar2226
    @nikhilbabar2226 2 года назад +1

    Question: How does the tabulation cover the case of picking multiple elements?

    • @sagartaak4017
      @sagartaak4017 Год назад +1

      bro u use dp[i][t-weight[i]] in pick case
      and in not pick use dp[i-1][t];
      and if you want to know working watch old videos in which striver sir explain it deeply
      or dry run for small values you get it easily and never forget then if you DIY

  • @saumilaggarwal5233
    @saumilaggarwal5233 Год назад

    thnx, was able to do this by myself

  • @sameersahu4569
    @sameersahu4569 2 года назад

    Understood Striver!!!Thank you for the amazing content

  • @riderpd09
    @riderpd09 2 года назад +5

    Why the forwoard iteration of wieght for using 1-d array not causing any problem?like dp 19

    • @takeUforward
      @takeUforward  2 года назад +9

      Because we need the current guy’s backward data and not previous guy’s

    • @sushant8686
      @sushant8686 2 года назад

      @@takeUforward in 19 th also we wanted curr guys backward na so why error there

    • @abhimanyusingh6445
      @abhimanyusingh6445 2 года назад +11

      @@sushant8686 in 19 , we were no in the same index after taking a particular element , consider the following statement of taking a particular element of dp 19 and this video :-
      dp 19:
      take = value[ind]+help(ind-1,w-weight[ind],weight,value,dp);
      dp 23:
      take = profit[ind]+help(ind,w-weight[ind],profit,weight,dp);
      in case of dp 19 , we are going to ind-1 , and for that , in our tabulation code , we will use prev array , in case of dp 23 we are in same index , and in our tabulation code , we will use curr array , if we are using curr , we do not need to worry about prev[w-weight[ind]], that is why forward iteration will work here .

    • @nishantsharma1042
      @nishantsharma1042 2 года назад +1

      @@abhimanyusingh6445 thanks bhai you have cleared my doubt🙏🙏

  • @nijalpatel5281
    @nijalpatel5281 2 года назад

    solved it by myself. Thank you striver.

  • @rishabhagarwal8049
    @rishabhagarwal8049 2 года назад

    Understood Sir, Thank you very much

  • @aruna5869
    @aruna5869 7 месяцев назад +1

    understood :)❤

  • @gourishettyvivek4153
    @gourishettyvivek4153 2 года назад

    thank you very much striver i have solved this sum by my own

  • @nikhilpandeydigital
    @nikhilpandeydigital Год назад

    understood !!!
    can you please tell me, do we really need to space optimize to a 1-d solution during the interview or it is optional?

  • @ketonesgaming1121
    @ketonesgaming1121 8 месяцев назад

    base case for tabulation should be for (int i = 0; i

  • @KunalTambe-oy6lb
    @KunalTambe-oy6lb Год назад

    UNDERSTOOD!!!!
    AND LOVED IT!!

  • @snehagoyal4978
    @snehagoyal4978 Год назад

    Thanks striver, was able to do it in all four ways by myself after sincerely following your playlist. ❤

  • @abdalla4425
    @abdalla4425 10 месяцев назад

    UNDERSTOOD BABY!

  • @vatsalvasoya5243
    @vatsalvasoya5243 Год назад

    Very well explained!!

  • @dennyage4791
    @dennyage4791 2 года назад +10

    Striver, we need problem where we can't use 2D grid , we have to use hashmaps for memo...

    • @mikelan9854
      @mikelan9854 2 года назад

      how are those different? maybe we should be able to solve them using maps to store. Or am i missing something?

    • @thinkingmad1685
      @thinkingmad1685 2 года назад

      @@mikelan9854 I guess the approach will be same just converting the 2d variables in form of string and storing it

    • @mahesuresh2443
      @mahesuresh2443 2 года назад

      @@thinkingmad1685 2d array is efficient as using map might give tle some times

  • @prakartigoel8711
    @prakartigoel8711 2 года назад +3

    Bhaiya ,why are we not writing the base case for weight parameter here ? how will the Take call terminate as the index is not decreasing , the weight is ,but there is no base case for weight ?

    • @dipanshusharma9084
      @dipanshusharma9084 2 года назад +2

      we don't have base case for that but we do have restriction for recursion call , that is when bag weight is less than the weight of item we will stop taking items and for that particular call take will be zero
      Make recursion tree to understand more....

    • @_singh516
      @_singh516 Год назад

      thanks for asking this . I also have same doubt

  • @kumarpurushottam632
    @kumarpurushottam632 Год назад

    Understood Thanks You Striver 😍

  • @ritik1857
    @ritik1857 2 года назад +1

    Wow 🔥🔥
    Bhaiya, How many videos will be there in total?

  • @shubhamtripathidev
    @shubhamtripathidev Год назад +1

    buying vegetables is the variation of ninjas training on gfg