DP 50. Minimum Cost to Cut the Stick

Sdílet
Vložit
  • čas přidán 18. 04. 2022
  • Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
    Problem Link: bit.ly/3rWLMnC
    Please watch the video at 1.25x for a better experience.
    Pre-req for this Series: • Re 1. Introduction to ...
    a
    Make sure to join our telegram group for discussions: linktr.ee/takeUforward
    Full Playlist: • Striver's Dynamic Prog...
    In this video, we solve the MCM Dp, this is the first problem on the pattern Partition DP.
    If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.org/interviews/s...
    You can also get in touch with me at my social handles: linktr.ee/takeUforward

Komentáře • 302

  • @sarthakchauhan8386
    @sarthakchauhan8386 Před 2 lety +82

    After getting this far in this DP series, I can say that I am starting to get ideas before you explain them. Thank you, Striver. You're the best teacher.

  • @champion5946
    @champion5946 Před rokem +28

    I don't know why but...
    MCM and this Video it feels like I am just seeing and memorising the solution..
    Maybe Partition DP is not for me... And I was thinking to Learn digit dp.. lol 😂

  • @shinewanna3959
    @shinewanna3959 Před rokem +22

    u did great exaplantion, learned a lot, if u set j = i in j loop, then u don't need to check if (i < j), just share my thought

  • @atifmirza9168
    @atifmirza9168 Před 9 měsíci +10

    00:00 Find the minimum cost to cut a stick given a cuts array and length of the stick.
    04:07 The video sponsor is HyreX, an app for finding startups to work for.
    07:55 To solve the problem independently, the array must be sorted.
    11:46 Algorithm to find minimal cost for stick cutting
    15:36 Algorithm for finding minimal cost partition
    19:16 Explanation of how to get the length of a stick easily
    22:46 Tabulation is the way to optimize the time complexity of the algorithm.
    26:39 Learn how to write tabulation in opposite order

  • @girishbhargava6367
    @girishbhargava6367 Před rokem +20

    Striver, you don't need the DP array of memoization to be of size C, it can be of n size also. It would work absolutely fine.
    Such is the beauty of this amazing playlist, our mind is itself writing the code and thinking of the logic of the problem.

    • @parthsalat
      @parthsalat Před rokem +13

      But an array of size N will take up more space than an array of size C

    • @mayanksharma2039
      @mayanksharma2039 Před rokem +4

      try running that in lc and you will get a memory limit exceeded

    • @07rockzz92
      @07rockzz92 Před 11 měsíci +2

      bro thats because N>C, TRY taking DP SIZE OF 1E5 THEN AND IT WILL ALSO WORK

  • @neelabhabanerjee8247
    @neelabhabanerjee8247 Před 2 lety +13

    Was eagerly waiting for this one. Its a very confusing question. Thanks a ton

  • @stith_pragya
    @stith_pragya Před 6 měsíci +1

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

  • @satyampriyadarshi6455
    @satyampriyadarshi6455 Před 2 lety +11

    Just in case someone is wondering why sorting, its just to make the formula for length to work otherwise we will not be sure always that arr[j+1] > arr[i-1]. Btw loved the explanationation.

  • @suchithreddy733
    @suchithreddy733 Před rokem +9

    Asusual excellent work striver and thanks a lot for this!! Shouldn't we be starting from j=i to j

    • @priyanshkumariitd
      @priyanshkumariitd Před 20 dny

      Yes, if you start with j = 1 -> n, then there is no need of writing if(i > j) continue; inside the loop.

  • @DevashishJose
    @DevashishJose Před 6 měsíci

    Understood , thank you so much Raj

  • @adityagoel7376
    @adityagoel7376 Před 11 měsíci +1

    i dont know why this golden channel has this few subscribers. I am what else are yu searching for bow down to this king!!!

  • @ishangujarathi10
    @ishangujarathi10 Před rokem +1

    Superb Explanation!!! espcially the cost logic

  • @bhaskarmishra8479
    @bhaskarmishra8479 Před 2 lety

    Amazing explanation!

  • @UECAshutoshKumar
    @UECAshutoshKumar Před měsícem +1

    Thank You
    Understood!!!

  • @aadishjain473
    @aadishjain473 Před 11 měsíci +18

    Sir Tabulation ma feel nahi aa rahi ha........Bina Memoization ka tabulation nahi likh payaga......
    Aur bass Memoziation --> Tabulation convert karta bass baan raha ha....Internally kasia kaam kar raha ha uska koi idea nahi ho raha ha

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

    Even though I still have some points to think carefully, I understood overall idea. Thank you sooooo much as always!

    • @jambajuice07
      @jambajuice07 Před rokem

      what do you mean by "Even I"

    • @cinime
      @cinime Před 11 měsíci

      @@jambajuice07 That should be "Even though I", my bad....

  • @ratinderpalsingh5909
    @ratinderpalsingh5909 Před rokem

    Understood, sir. Thank you very much.

  • @xolo2617
    @xolo2617 Před rokem +1

    I solved this problem without this video but I didn't solved optimally , my soution was of somewhere around 400 ms , after watch I will clear all doubt

  • @rishabhagarwal8049
    @rishabhagarwal8049 Před rokem

    understood sir, Thank you very much

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

    Thankyou sir. Understood

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

    As always "understood" ❤️

  • @gauravmehta4011
    @gauravmehta4011 Před rokem +4

    when we did the recursion, the base case was (i>j) return 0; but incase of tabulation why is j starting from 1 instead of i +1

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

      you can start the j from i. (not i +1 as u mentioned, because when i == j, it does not satisfy base condition)

  • @akashsahu2571
    @akashsahu2571 Před rokem

    yes

  • @sameersahu4569
    @sameersahu4569 Před 2 lety

    Thank you

  • @ganeshkamath89
    @ganeshkamath89 Před 2 lety

    Thanks Striver. Understood

  • @1tav0
    @1tav0 Před rokem

    this was awesome great explanation us

  • @JitendraKumar-ti6yd
    @JitendraKumar-ti6yd Před rokem +2

    isn;t this the vairations of MCM based DP? I found the solution similar to Burst ballons

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

    Best explanation ever

  • @preetisahani5054
    @preetisahani5054 Před 9 měsíci +1

    Understood it very nicely, What was in the back though at 11:55 XD

  • @iamnoob7593
    @iamnoob7593 Před 6 měsíci

    US striver , First difficult one in this playlist

  • @adarshmishra7113
    @adarshmishra7113 Před rokem

    understood as always

  • @dheerajbhardwaj6086
    @dheerajbhardwaj6086 Před 2 lety

    Thanks

  • @Aditya-rs5dj
    @Aditya-rs5dj Před rokem

    amazing explanation

  • @parthstark2934
    @parthstark2934 Před 2 lety

    nice question

  • @prabhakaran5542
    @prabhakaran5542 Před 2 měsíci +1

    Understood❤❤❤

  • @ashvinkumhar5819
    @ashvinkumhar5819 Před rokem

    As always the best One!!

  • @techdemy4050
    @techdemy4050 Před 2 lety +7

    I have doubt I still didn't understand how sorting is relevant [10:45] if 2 is at the end after 5 then won't we take cost 2 because we have [5,2] in the second half of the array. Do you mean cuts here is the cut making on the number line 0 to N thats why we have sorted. For ex: If we making a cut 2 from cuts array are we making a cut on that number line [0 to N] position 2. Am I understanding the question correctly??

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

      Yes, so basically u are making a cut on a stick!

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

      @@takeUforward Thank you so much for making amazing videos with great explanations and clearing my doubt, loving this DP series

  • @satyampande684
    @satyampande684 Před 2 lety

    understood!!

  • @vinayverma8897
    @vinayverma8897 Před rokem +1

    how to print that order of cutting the sticks so that cost is minimized?

  • @arihantjammar8888
    @arihantjammar8888 Před 9 měsíci

    Understood 😊

  • @jm_ohit
    @jm_ohit Před 3 dny

    same question came in codeforces contest currently.

  • @wigglesort
    @wigglesort Před rokem

    Can anybody please explain why we are sorting the cuts array. I am not getting it.

  • @aryansinha1818
    @aryansinha1818 Před rokem

    Why are we using cuts.push_back & cuts.insert?

  • @anshumangupta1001
    @anshumangupta1001 Před rokem

    Understood.

  • @LBK3
    @LBK3 Před rokem

    understood ❤

  • @divyachopra2369
    @divyachopra2369 Před rokem

    How is this question different from the question min cost to cut the rod in n parys

  • @dss963
    @dss963 Před rokem

    The time complexity for memoization and tabulation will be o(n^2). Despite, there is a loop inside , we would be calculating the (i,j) subproblem only once.

  • @sahild5953
    @sahild5953 Před 11 měsíci

    can someone explain why is there a need to sort the array?

  • @dhrubajyoti3774
    @dhrubajyoti3774 Před rokem +1

    Understood

  • @oblivion_5910
    @oblivion_5910 Před 2 lety

    understood

  • @tikshavarshney213
    @tikshavarshney213 Před 2 lety

    Understood !

  • @alfredeinstein1742
    @alfredeinstein1742 Před rokem +4

    Please tell how did you identify this problem is of MCM and how's this problem different from unbounded knapsack problem of rod cutting

    • @vaibhavsaharan7898
      @vaibhavsaharan7898 Před 6 měsíci +1

      I think if we assume that the costs are based on the index of the cut, then it's a MCM variation whereas If costs array is given for the length of a cut piece, then it becomes a unbounded knapsack problem variation

    • @priyanshkumariitd
      @priyanshkumariitd Před 20 dny

      costs are based on the length to which a cut belongs...so, there are many patterns for re-arranging cuts...Hence, it's a MCM variation

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

    in Tabulation in the 2nd loop why didn't we initialise j from i+1 like we did in mcm ques. ??

    • @gv_1010
      @gv_1010 Před 2 lety +2

      the condition i > j will take care

  • @pulkitranjan8189
    @pulkitranjan8189 Před 2 lety

    till this video I understood all videos

  • @its_me136
    @its_me136 Před rokem +1

    finally solved at my own ☺☺

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

    In the tabulation code why can't we do for(int j = i + 1; j j) continue;
    I tried it but it is giving a wrong answer. Can Someone explain why?

    • @vanshsehgal9475
      @vanshsehgal9475 Před 2 lety +10

      bro u need to start j from i, as i can be equal to j.

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

    in the tabulation code, why are we taking j from 1 to n instead of i+1 to n as j should always be on the right of i like previous mcm problem. pls help

    • @altamashsabri8142
      @altamashsabri8142 Před 2 lety

      yes you can start j from i to c.......so basically for ( j = i ; j

  • @girikgarg8
    @girikgarg8 Před 2 lety

    Can the base case be if (i==j) condition

  • @rahatsshowcase8614
    @rahatsshowcase8614 Před 2 lety

    understood ! awsomme!!

  • @coding8000
    @coding8000 Před 2 lety +2

    Thanks, mam , eagerly waiting for this, please upload more frequently. ,please.

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

    DP Revision:
    This MCM part still feels difficult, had to watch the video to get the logic ;-;
    Nov'22, 2023 03:19 pm
    (Late due to 2 endsems exam: today morning and yesterday's afternoon.)

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

    striver can you make a summary video of vids of 50-57 to make it more clear

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

    can u show how dp table gets filled

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

    Please make a series on dp on stone games later

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

    In the previous question j was i+1 but here it isn't. Why? In tabulation?

    • @sahilsaini4574
      @sahilsaini4574 Před 3 měsíci

      i have the same doubt, do you know the answer now?

  • @yeswanthh5068
    @yeswanthh5068 Před rokem +1

    Understood 🙂

  • @sauravchandra10
    @sauravchandra10 Před rokem +1

    I dont think I was able to get the approach intuitively, which proves I still have a long way to go.

  • @cristeinfuze8574
    @cristeinfuze8574 Před rokem

    Anyone plzz clear this if we have to choose where we made cuts how we can do and print this is in order ?

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

    @take U forward -> I have still confusion in dp array length , as here also i going from 1 to m then total element is m-1+1=m then why you are taking dp[m+1][m+1] ,why not dp[m][m]

    • @kapilsingh2816
      @kapilsingh2816 Před 2 lety +2

      arrays are 0 based indexed always so if we skip 0 index and want 1-based indexing so
      we will have to take an array of n+1 size as arr[0] will always exist there we just shifted our
      starting index to 1 so ending index should also be shifted by 1 i.e. n+1 and
      If 4th index is last index so size will be 5.

    • @pranavsharma7479
      @pranavsharma7479 Před 2 lety

      @@kapilsingh2816 i is going till n but j is not going till n

  • @awggeez
    @awggeez Před rokem

    Why do we return dp[1][c]?

  • @Nitro-kx7ok
    @Nitro-kx7ok Před rokem

    understood sir🙏🙏❤❤

  • @jyothiyadav2595
    @jyothiyadav2595 Před 7 měsíci

    How the for loop os working i didn't understand that part, even when u reversed i and j value while calculating the cost how is it same as recursion , that cost cal how is it working ???

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

    why the length is cut[j+1] - cut[i-1] and not cut[j+1] - cut[i-1] -1 because I think that gives the actual length between 12 and 3 i.e 8

    • @chetanraghavv
      @chetanraghavv Před 2 lety +2

      The length is after the cut is made at length 3 from start so if originally length of stick was 12, then length after the cut should be 9 (12-3)

    • @saunaknandi1814
      @saunaknandi1814 Před 2 lety

      @@chetanraghavv got it

  • @VikashYadav-px8ei
    @VikashYadav-px8ei Před rokem +1

    Understood 🎉

  • @Raj10185
    @Raj10185 Před rokem

    ye kaun hi spch payega lets insert 0 and length of rod in cut arr then for finding the length do that stuff really it is tough

  • @vinayyelleti2714
    @vinayyelleti2714 Před rokem

    In the first approach u mentioned aux space ...
    can u pls tell where is that extra space

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

    Hello Striver base case i>j not cleared😩

  • @yashmamidwar6729
    @yashmamidwar6729 Před 2 lety

    Understood !!

  • @parshchoradia9909
    @parshchoradia9909 Před rokem

    Understood Sir!

  • @bahveshjain1730
    @bahveshjain1730 Před rokem

    Striver why you didn't take j from i+1 as in the previous video of MCM tabulation j should be in the right of i?? Please reply 🙏

    • @kiransequeira6152
      @kiransequeira6152 Před rokem +2

      if you observe the inner loop, the loop just executed the 'continue' statement for all j=i (i.e same as the inner for loop from j=i to c)

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

    Just a doubt instead of taking cuts[j+1]-cuts[i-1] to get the length can we pass the initial length n=7,and for
    initial f(i,idx-1,arr[idx]) and f(idx+1,j,n-arr[idx]) ?

    • @hrithiksomani
      @hrithiksomani Před 2 lety +7

      1. Try to think why sorting was done.
      2. Watch the video from 18:30-19:30 might help.
      As per my understanding:
      The left subproblem or the left recursion where you are passing arr[idx] will that work ? Or it should be arr[idx]-value at previous_position_where_it_was_cut ? Think on the second or third level of recursion not only on the first level.

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

      I thought the same, but this will increase the space complexity.
      Striver's method is better.

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

      see n is bounded by 1e6. so it will give you tle and mle if you take n as a state that is why add n in the array itself and in worst case size of array would be 1e3+1 which will cause no harm.

  • @shikharbansal2942
    @shikharbansal2942 Před 2 lety

    Just brilliant

  • @original_gangsta_
    @original_gangsta_ Před 2 lety

    UNDERSTOOD

  • @anthonyjaurique1979
    @anthonyjaurique1979 Před 2 lety

    Is the video getting blurred for anyone else ?

  • @kongzilla2897
    @kongzilla2897 Před 2 lety

    Problem link is Not working, Please look into it

  • @_inspireverse___
    @_inspireverse___ Před rokem +1

    You are awesome❤❤

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

    Striver u havent attached the notes for the videos from 40 plz do it helps during revision

  • @user-yd9xf2ss3m
    @user-yd9xf2ss3m Před 6 dny

    us bhaiyaa after couple of attempts..

  • @ec-223ujjawalkumar5
    @ec-223ujjawalkumar5 Před 2 lety +1

    plzz reduce your on screen cam size to a square one it's difficult to focus

  • @apurbanath4822
    @apurbanath4822 Před rokem

    I have a small doubt, why are you taking the base case of: if(i > j) return 0; and not if(i >= j) return 0; because if i == j, we will have a stick of unit length, and cutting it won't be possible.

    • @kartikeyjangir6003
      @kartikeyjangir6003 Před rokem +1

      Base Case: As i j this is not a valid partition so we can return 0.

    • @ritvikverma1443
      @ritvikverma1443 Před 11 měsíci

      if i==j, it doesnt means, we have a stick of unit length, because we are iterating over the cuts[ ] array, so if i==j, means, that we are allowed to cut only at a single place (value of i or j), hence we can use the same formula in this case also for taking out the length of the stick, which is : cuts[j+1]-cuts[i-1]. then the length will be calculated for i==j case

  • @aakashgoswami2356
    @aakashgoswami2356 Před rokem

    I did n't get sorting part :(

  • @mohitsinghnegi9677
    @mohitsinghnegi9677 Před rokem +1

    What if 0 and length of stick is already included in cut array, then this solution won't work . We should not insert 0 and length if already exist

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

    Sir please make a video on scramble string

  • @souvickmazumdar8129
    @souvickmazumdar8129 Před rokem

    Understood 😃

  • @googleit2490
    @googleit2490 Před rokem

    Understood :)
    Aug'2, 2023 11:10 pm

  • @SajanKumar-ec2us
    @SajanKumar-ec2us Před 2 měsíci

    tell me time complexity of tabulation

  • @vaishnavithakur6460
    @vaishnavithakur6460 Před 2 lety

    Understood!!!!

  • @Noob_Coder1234
    @Noob_Coder1234 Před 5 měsíci +1

    #include
    int f(int start ,int end , vector &cuts , vector &dp) {
    if(dp[start][end] != -1)
    return dp[start][end];

    int value = 1e9 , flag = 0;
    for(int i = 0; i < cuts.size(); i++){
    if(start < cuts[i] && end > cuts[i]){
    flag = 1;
    int val = (end - start) + f(start , cuts[i] , cuts , dp) + f(cuts[i] , end , cuts , dp);
    value = min(value , val);
    }
    }

    if(flag == 0)
    return dp[start][end] = 0;
    return dp[start][end] = value;
    }
    int cost(int n, int c, vector &cuts){

    sort(cuts.begin() , cuts.end()) ;
    vector dp(n+1 , vector (n+1 , -1));
    return f( 0 , n , cuts , dp);
    }
    I SOLVE USING THIS CODE ITS BIT DIFFERENT IT GOT accepted on coding ninjas but its giving memory limit exceeded error on leetcode can anyone tell me whst is the reason ?

  • @amancuber5235
    @amancuber5235 Před rokem

    I am glad you said it a hard problem! LOL

  • @sathyakrishnanthirunavukka7908

    US