DP 20. Minimum Coins | DP on Subsequences | Infinite Supplies Pattern

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

КОМЕНТАРІ • 760

  • @boyidapumohit4715
    @boyidapumohit4715 2 роки тому +81

    This series isn't just explaining DP. It's more than that. Initially, for every problem, I used to tabulate and spend lots of time thinking about states, and transitions. But while following these Lecs, I got to know a path to achieve the solution, Now even in interviews, this helps me as I could explain recursive approach to the interviewer, which is very intuitive. Thankyou Striver.

    • @GajendraSingh-lv3jw
      @GajendraSingh-lv3jw 2 роки тому

      bro can we use knapsack approach to reduce the space complexity more?

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

      @@GajendraSingh-lv3jw or kitni reduce karega guru 😂

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

      @@GajendraSingh-lv3jw
      public int coinChange(int[] coins, int amount) {
      int dp[] = new int[amount + 1];
      Arrays.fill(dp, amount + 1);
      dp[0] = 0;
      for(int coin : coins)
      {
      for(int j = coin; j amount ? - 1 : dp[amount];
      }

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

      ya bro we cant directly dive to tabulation but once we write recursion then it is so easy

  • @NavaneethaKrishnan-g6x
    @NavaneethaKrishnan-g6x Рік тому +13

    Facing DP problems: No fear... Striver is here...🤩 thankyou bro.

  • @udaypratapsingh8923
    @udaypratapsingh8923 2 роки тому +302

    *that 34 minutes is way more precious than my whole college day*

  • @deepanshudhakate9622
    @deepanshudhakate9622 2 роки тому +81

    Now this DP SERIES is going as per expectations.
    As always
    UNDERSTOOD 🔥 😊

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

    There is no any Substitute of Your content on UA-cam.
    U r great ❤️

  • @divyambhutani6229
    @divyambhutani6229 2 роки тому +33

    Thank you bro . Felt very good , was able to attempt a dp question in one of the contest today . I am happy to witness my growth by watching your dp playlist . thanks very much 🙇

  • @CEOplays
    @CEOplays 2 роки тому +14

    We can solve it using only one array :
    vectorprev(x+1,0);
    for(int i=0;i

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

      unlike in previous question for single array optimization, why didn't we changed inner loop from --> for(int j=x; j>=0; j--) ?

    • @MohakNigam-q5d
      @MohakNigam-q5d 2 місяці тому +1

      @@spiral546 Let me explain the key distinction:
      0/1 Knapsack Problem:
      In the 0/1 Knapsack problem, you can either include an item or exclude it, but you can't include an item multiple times. Each item can only be chosen once. That’s why we loop backward in the knapsack problem, to avoid overwriting values that still need to be used in the current iteration.
      For example, let's say we're considering adding an item that weighs w:
      If you loop forward, you might accidentally update the result for a smaller weight using the current item (which you shouldn't).
      But if you loop backward, you safely update the values for higher weights first, leaving the smaller weights unchanged until they are processed in the next iteration.
      So, in the 0/1 knapsack, backward looping ensures that each item is only used once.
      Coin Change Problem:
      In the Coin Change problem, the difference is that each coin can be used multiple times. Therefore, for each coin, you want to use the result of smaller amounts that include previous uses of the same coin.
      If you loop backward, you might use a value that has already been updated in the current iteration, which can lead to using the same coin more than once in an incorrect way.
      Summary of Differences:
      0/1 Knapsack: Each item can only be used once, so you loop backward to ensure that earlier values (smaller weights) aren't affected by current updates.
      Coin Change: Each coin can be used multiple times, so you need to loop forward to ensure that you're using values from the previous iteration and not overwriting them prematurely.

    • @deekshithavarma3997
      @deekshithavarma3997 Місяць тому

      j loop should be reverse order

    • @spiral546
      @spiral546 15 днів тому

      @@deekshithavarma3997 refer to @mohaknigam solution

  • @jaishriharivishnu
    @jaishriharivishnu 5 місяців тому +2

    3:53 greedy doesn't work because, let suppose x is a number greater than ith coin... now it is not guaranteed that if we make x using coins index < ith coin , we will be encountering the value of ith coin while approaching the x... Denomination like 1,2,5,10,20,50,100.... follow this, let suppose we want to make 59... let suppose we didn't choose 50 rupee...now if you try to make 59 using coin

  • @arnabdutta4662
    @arnabdutta4662 2 роки тому +58

    one more base case can be added -
    if( target == 0 ) return 0; -> in the memoization
    for tabulation -> we can simply start the 2nd loop from 1 to target
    no need to fil for zero as by default its value will be stored as zero.
    Hope it helps :)

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

      if you dont mind, can you send the code for tabulation

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

      @@subhashpabbineedi7136 it is not mandatory to add this base case

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

      ​​@@subhashpabbineedi7136 fick tabulation only memoization

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

      the if condition will take care of this in recursion and memoization

    • @jitendrakumar-vv8ho
      @jitendrakumar-vv8ho 5 місяців тому +1

      Actually i was wondering why t=0 case has not been taken care of

  • @AquaRegia-i3u
    @AquaRegia-i3u Рік тому +15

    Interesting Fact 🌟
    So DP helps to find minimum no. of change notes/coin 💵 I get when I go for shopping. But we don't see a shopkeeper applying DP just to return the change. How does it happen that the shopkeeper always returns the minimum no. of notes 💵as change?
    If you observe they apply greedy approach. But how does greedy give correct answer? Its because our currency denominations (Rs 10, Rs 20, Rs 50, Rs 100.....so on)💵 are such that the greedy approach always gives the optimal answer. Quite Interesting 💡

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

    Time complexity can be explained like this. O(2^Max(n, amount/min(coins))
    e.g [1,2,3,4] , amount = 12. amount/min(coins) = 12/1 = 12, n=4, Max of the 2=> 12. Therefore O(2^12)
    Please correct me if I am wrong.

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

      yes its correct, but we can say its exponential in nature

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

      but here for index=0 the function would directly return target%arr[0]. for other index it complexity should be what you mentioned

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

    I love watching these 30 min videos that spend 40 minutes making notes !

  • @OMPRAKASHSINGH-j5l
    @OMPRAKASHSINGH-j5l Рік тому +4

    Thank you for existing striver ! Helping others is the best thing any human can ever do! U are the best of our kind.

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

    done this before 3 month after 3 month i Tried this question and boom got this easily u teach very well 👊👊

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

    Getting better at DP day by day Thankyou Striver!!!

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

    UNDERSTOOD.............Thank You So Much Striver Bhaiya for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

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

    Understood to the Highest Level !!!!!!! Thank You Sir.

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

    Last stage optimisation to use single array:
    class Solution {
    public:
    int MX = 1e5;
    int coinChange(vector& coins, int amount) {
    if (amount == 0)
    return 0;

    vectordpo(amount+1,0); // change vector dimension: use single array. Reason: No need to maintain 2d array. Use in line replace to basically
    // access same information.

    for(int i = 0; i

  • @gaura-krishna
    @gaura-krishna 2 роки тому +4

    One of the best decisions that I've made to learn programming is this... Watching your videos Striver...! One day maybe I'll make even better videos...

  • @uttejdunga9795
    @uttejdunga9795 6 місяців тому +1

    1D optimisation is great , loved it 👍

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

    Understood!! This DP series is magical! 💫🧿

  • @yaserahmed9721
    @yaserahmed9721 2 роки тому +33

    Wow i solved this problem today morning same way striver taught the previous topics!! You are rocking man

  • @GManiKarthik
    @GManiKarthik 17 днів тому

    #Understood #DP20 #Hatsoff #Striver
    Did this question on my own post the step of "STAYING AT SAME INDEX". Man this series is literally 🔥 🔥

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

    understood...u r messiah in human form...best dsa content on youTube for sure.

  • @a_maxed_out_handle_of_30_chars

    this felt good to watch, thank you :)

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

    Most tutorials teaches how to memorise the formulae to solve DP questions but with Striver you get to understand the how thing and you can solve any problem even one that you have never encountered before.

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

    Understood!
    Also, we can solve the problem using just a single array (instead of two).
    int minimumElements(vector &num, int tar)
    {
    int n = num.size();

    vector prev(tar+1);

    for(int j=0 ; j

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

    understood, i was able to write the recurrence solution without watching the vedio, thankyou striver

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

    Totally understood the concept, you made it easy for us! Thanks for all your efforts.

  • @AquaRegia-i3u
    @AquaRegia-i3u Рік тому +6

    Quick Tip 💡 : We can space optimise it using a single array like previous question. Also, we don't have to start inner loop from back this time.

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

      Insightful.
      also i mention that not only that reduces SC but TC also , because inside the first loop we don't have to copy the prev as curr which is 0(n) extra in multiplication. the Tc reduced to approx : (target+1)*(n) from (target+1+n)*(n).
      the reason because of this is when we are in same level we are independent of previous array values, as they could be used but we are checking the same coin if it can fit in again. so ind-1 is not necessary and when we not take there is no need to push any value in curr thus not needing it.

    • @dsp-io9cj
      @dsp-io9cj Рік тому

      yes, but why shd we go from left to right, im getting wrong ans if j frm sum to 0. I still feel it shd be frm sum to 0, coz the values are dependent on previous values like prev[j-num[i]]., getting wrong ans. explain y shd we go frm 0 to sum

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

      did you find answer for your query?@@dsp-io9cj

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

    Thanks Striver

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

    similar to combination sum in your recursion playlist so this was easy for me !Us

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

    I now know this is obvious but you could have told us why didn't you write base case for the second state T as well, it returns 0 when T == 0 , but would have been more complete that way, thanks for the video.

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

      man did you get the answer coz i am wondering about this as well .why its rejection does'nt cause any problem to the code

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

      it wont affect the code as think if target becomes zero before hitting index 0 , the only recursive call of not take will run , and it wont affect the number of coins !
      as there is a check before picking the coin arr[ind]

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

    Thanks for explaining with so much clarity. Able to do all recursion, memoization, tabulation and finally space optimization :)

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

    Can bound the time as O(2^(N+T)) where T is the total we need. Because at max each coin can be picked T times (if coin is value 1, else less times). And space O(N+T)

  • @JustGyan
    @JustGyan 6 місяців тому +1

    why we are not considering the base case of when target become zero?

  • @ashwaniagrawal27
    @ashwaniagrawal27 3 місяці тому +1

    this question blew my mind

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

    Understood, thank you so much sir for such content, trust me koi ye level de he ni sakta jo aap free me de rhe hai , mujhe dp khi smajh aya to TUF pe,thank you :)🙏

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

    *Complexities*
    Recursion: 21:20
    Memoization: 23:00

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

    His way of teaching never discriminates with a backbencher

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

    Thanks!

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

    For me , it is the best yt channel , for coding related stuff❤

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

    For the recursive approach why don't we have a base case for (T==0) return 0; cuz as we can see at each stage we are either decreasing ind or target.
    But the code seem to work even without (T==0) case, WHY??

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

    very much similar to combination sums that u taught..#striver takes us forward

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

    I solved first dp question by myself ..ty striver ❤️❤️❤️❤️

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

    Clean and clear explanation ☺️☺️.
    Understood 👍🏼👍🏼

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

    one doubt -> so here why can'nt we use the sorting on the coins[] array and start dividing it from the last by doing the soriting we can form the uniformity so we can use greedy isn;t it ? cause the order of the coins does'nt matter in this case

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

    better to write if(target == 0){return 0} as base case as well in order to avoid stack overflow. Also I wonder how was this working without this base case

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

      I was thinking the same. Thanks for pointing it aloud.

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

      when target = 0, only the notTake call is made until ind==0. For ind==0, target%x is true thus target/x is return which would be 0. So return 0 case is still working though stack space will be wasted as you pointed out.

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

      @@anuragprasad6116 but what if target is 0 and index is not 0?

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

      @@GirishJain if target is 0 then it will not pick anything till index 0 and hence at base case ind==0, target%x is true thus target/x is return which would be 0.

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

    Understood! Thank you for your detailed explanation as always !!

  • @AmanKumar-qz4jz
    @AmanKumar-qz4jz 11 місяців тому +1

    i think ur god.....u came here on earth for us!!!

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

    UNDERSTOOD !!! 🔥🔥🔥🔥🔥🔥
    As in DP19 similar this problem can also be done using only one single array ...

    • @MohammadKhan-ld1xt
      @MohammadKhan-ld1xt 2 роки тому +1

      Here we require the curr array also as in tabulation you could see that for pick we were writing:
      pick = dp[index][sum-arr[index]];
      So index is not decreasing, that means we require the curr array.
      pick = curr[sum-arr[index]];
      In the previous question we did not require the curr array for current array(curr) to be computed.
      pick = dp[index-1][sum-arr[index]]; OR pick = prev[sum-arr[index]];
      Hope this helps!!!

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

      @@MohammadKhan-ld1xt I think we can do this using 1 array as well. Hint: just see that we only need one element from the prev array and that would already be stored in the single array we will be using. Also we go from left to right.

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

      @@vaibhavkaul2029 Yes, you're right. Thanks for this comment. You saved my time...

  • @arjunkhanna1803
    @arjunkhanna1803 2 роки тому +5

    NOTE FOR SELF:
    When sum of smaller coins any of the larger coin then dp

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

      @Arjun Khanna Great! could you explain the intuition behind the above conclusion!

    • @AMANKUMAR-eq6sq
      @AMANKUMAR-eq6sq 2 роки тому +2

      ​@@maneeshguptanalluru7807 Greedy works perfectly in the case where law of uniformity is followed otherwise DP

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

    Understood...Completed (20/56)

  • @shivamchauhan2267
    @shivamchauhan2267 2 місяці тому

    Greatly explained

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

    I have a doubt, the base condition when ind==0 , when this base case is reached from function call of not take ,code is returning value/coins[ind] for not take function call, but not-take means we are not taking a single coin, so how does it work?

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

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

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

    Thanks a lot Striver, another question I was able to solve without seeing video just because of your old videos

  • @402saravana4
    @402saravana4 Рік тому +3

    Dear Striver,
    In this problem, we are not checking the base case which occurs when the target is equal to 0 and index is not zero. This happens if ar=[1,2,3] and target = 9; then the index will stand on index 2 , recursive calling 3 times and mean while target becomes 0. So I think we have missed this case.
    Am I correct or lost anywhere ? Please explain
    Thank You.

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

      See if it's stuck at index!=0 and target == 0 then it will give two calls of notpick & pick, pick will give 1e9. Notpick will give another function call, which might ultimately give 1e9 for both(pick,notpick). Check 19:30, he stopped (1,0) and if you dry run further for it you''d realise it gives 1e9 for both. Your test when run on codestudio gives the correct answer viz. 3.

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

    Why we are declaring the curr array outside of for loop unlike the previous questions where we did it inside the first for loop?

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

    Understood Bro :) One Row solution is awesome :)

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

    Here we can avoid that modulo calculation and just have N row where with amount 0 mark as 0 else INF..and also in 1d dp it can be reduced to one current only instead of prev and current..so this are some optimization that can be done but you're great teacher anyways..kudos to you for giving back to community

  • @prabhakaran5542
    @prabhakaran5542 9 місяців тому +1

    Understood ❤

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

    shouldn't we also check for target being 0 in the recurrence relation for when the target has become zero but we haven't reached the index 0?

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

      no need it will be covered as part of the case where we just not pick any thing till index 0 after the target became 0.

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

      Delhi Public School?

  • @AbhishekKumar-cv1dh
    @AbhishekKumar-cv1dh Рік тому

    Understooood 🔥🔥🔥🔥🔥

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

    int f(int target, vector &num, vector &dp){
    if(target==0){
    return 0;
    }
    int ans=1e8;
    if(dp[target]!=-1) return dp[target];
    for(auto ele:num){
    if(ele

  • @RajeshKumar._.16
    @RajeshKumar._.16 Рік тому

    for(int i=0; i

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

    Hey , striver before seeing your Lecture I tried this qsn by myself and was able to came up with a recursion technique -
    if(i==0)
    {
    if(t%num[i]!=0)
    return -1;
    if(t%num[i]==0)
    return t/num[i];
    }
    if(t==0)
    return 0;
    if(dp[i][t]!= -2)
    return dp[i][t];
    int notpick= 0 + rec(i-1,t,num,dp);
    int min_pick=INT_MAX;
    for(int c=1;c

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

      Bhai exactly same the technique as your then striver bhiya told this one and I feel function call stack will be same so i just search on leetcode and submit over there it will be getting submitting but acception rate less then striver bhai solution but they both are exactly same technique why this is happening

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

    Understood man! Amazing work! Keep up the good work

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

    I thought that I can replace 1e9 with Integer.MAX_VALUE, but that led to wrong ans, since we are adding 1 to it in one of recursive calls, and Integer.MAX_VALUE + 1 = Integer.MIN_VALUE, which led to wrong ans.

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

      I am curious - how and why does only 1e9 work? Can it be 1e8 OR 1e10?

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

      You can use INT_MAX, but do 1 + fn() only if result of fn() is not INT_MAX

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

      What's wrong with this?
      public static int minimumElements(int num[], int x) {
      return (int)minimumElements(num, x, num.length-1);
      }
      public static long minimumElements(int num[], int target, int i) {
      if(i == 0) {
      if(target % num[0] == 0) return target / num[0];
      return Integer.MAX_VALUE;
      }
      long take = Integer.MAX_VALUE;
      if(num[i] >= target)
      take = 1 + minimumElements(num, target - num[i], i);
      long drop = minimumElements(num, target, i-1);
      return Math.min(take, drop);
      }

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

    Thank you!!! Was waiting for this eagerly.

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx Рік тому

    UNDERSTOOD 🔥 😊

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

    One query,
    In tabulation if we go the other way round (In backward direction):
    Putting the snippet here:
    /* Here, x is our required sum and n is the size of array containing available coins*/
    for(int i = 0; i = 0; ind--)
    {
    for(int target = x; target >= 0; target--)
    {
    // Not pick
    int nPick = dp[ind+1][target];
    // Pick current
    int pick = 1e9;
    if(num[ind] = 1e9)
    return -1;
    return dp[0][x];
    /*
    Here, in this case I am getting correct answer for -1 cases but for all the other cases instead of getting the correct value, I am getting 1
    If I modify the logic to move from 0 to end in that case the code is working.
    Could you please explain or give a thread so that I can understand what exactly is the issue here.
    */

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

    Yesssss!!!. Did it on my own. Understood Sir ✅

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

    UNDERSTOOD ❤

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

    Thanks soooooooooooooo much Striver!

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

    Understood it clearly. Thank you so much.

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

    I solved this question on my own !!!😁

  • @AdityaPratapSingh-mb6jv
    @AdityaPratapSingh-mb6jv 4 місяці тому +3

    @takeUforward @everyone Can someone explains what the difference between the problem statement of DP 20 and 22? Because i think both the problem are same.

  • @MJBZG
    @MJBZG 15 днів тому

    could solve it on my own, thanks!

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

    Can we do this using 1D dp also. Like arr[]={9 6 5 1} amount =11 first in the loop we will check for 9 since it is greater than 11 now we will go for f(11-9) se similarly all others.

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

    Amazing!! Just tired of scrolling Leetcode discuss, here is the best answer, Thanks!!❤🔥 🙌 💥

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

    I had one question about this vs knapsack. Usually, when we want min/max of something, if the base case does not satisfy the condition we return max/min as we do in this problem. But in knapsack we returned a 0 instead of a min value. I don't understand when to return min/max vs 0. Any help would be appreciated.

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

      Hey, did you find the explanation to this?

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

      one is about the path and other is about value

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

    Why are we taking cur[T - nums[ind] ] instead of prev[T - nums[ind] ] in space optimization? As per my understanding, we have been using prev row before updating the cur row in each iteration.

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

    Thanks Striver!

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

    Understood, sir. Thank you very much.

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

    Understood Thank you so much.

  • @hrushikesh-1914
    @hrushikesh-1914 Рік тому

    Understood. Thankyou sir

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

    If we apply greedy in gfg question minimum no of coins it passes all test cases even in editorial they have mentioned greedy solution is working, may be because of denominations given in question the greedy might work. But if there is no uniformity why is greedy working?

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

    Understood and really thankful for your great efforts.

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

    You r the best teacher brother🔥

  • @yaxprajapati115
    @yaxprajapati115 7 місяців тому +1

    why can't we add the number of the coins picked up like this:
    pick = solve(coins, amount % coins[index], index-1, dp) + amount/coins[index];

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

    Striver i dont know whether you will see this, but your videos and SDE sheet have changed my life. I got selected as an intern today in a product company only thanks to you my friend. Love you striver bhaii!!!!!!!!!! You're the best ever youtuber i know

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

    understood// but doubt why tabulation fail while memoization passed because in the last part of space optimization we again using the tabulation which is passed but we know time complexity is same in both the cases.

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

    hey striver. i solved this question by same logic and solution got accepted on leetcode but it gave TLE and Run time error on CSES problem sheet.
    if you can look into this then it would be a great help for me. i didn't get the logic of other solns. And i do cp that's why i solved those problems.

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

    Awesome.....Loved it

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

    We can also do this using one parameter(amount) in recursion:-
    // Recursion Memoization Code for reference
    class Solution {
    private:
    int f(vector &coins, int amount, vector &dp) {
    if(amount == 0) return 0;
    if(amount < 0) return -10000000;
    if(dp[amount] != -1) return dp[amount];
    int ans = 1000000;
    for(int i=0; i

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

    am bit confused about the space complexity, in recursion and memorization , is it O(N) or O(T) , for the recursion stack

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

    Doubt: i tried the same technique but instead of taking "take" as 1 + helper() i kept count as a variable within the helper function and returned min(take, ntake). also, when amount==0 then i returned count instead of 0, accordingly i made all the changes. it worked for some cases not for others. can anyone explain why this happened?

  • @johnsimon8158
    @johnsimon8158 2 місяці тому

    Thanks for this awesome video

  • @094_sujithkumar4
    @094_sujithkumar4 Рік тому +1

    Sir I think writing the base case when target hits 0 would be some more optimal