14 Rod Cutting Problem

Поділитися
Вставка
  • Опубліковано 22 січ 2025

КОМЕНТАРІ • 448

  • @shubhamchaudhary8688
    @shubhamchaudhary8688 4 роки тому +393

    Kids -> Standard Problem
    Legends -> Praaacheen Problem 😂
    Amazing work dude.

  • @chaitanyagawande855
    @chaitanyagawande855 4 роки тому +69

    You are the only person who can explain things in the best way than others. No one teaches like that. You're a gem brother, because of you I have started loving DP

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

      tabulation method me initialization kaise karein?
      please anyone explain the logic!
      Thanks.

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

      @@abdulnafe6442 make dp of size n+1 and length +1. make elements of zero length of dp to be zero. And make element of zero size of dp to be zero.
      dp[n+1][L+1]; in two loops { if (j==0||i==0 then make dp[i][j]=0;}

  • @0anant0
    @0anant0 4 роки тому +157

    14 of 50 (28%) done! Very nice explanation! Repetition makes is easier to consolidate with each iteration :-)

  • @ishankiverma9450
    @ishankiverma9450 2 роки тому +7

    The way you have explained dp is remarkable and highly appreciable, please start teaching other topics as well.

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

      tabulation method me initialization kaise karein?
      please anyone explain the logic!
      Thanks.

  • @garimabhatia6511
    @garimabhatia6511 3 роки тому +12

    The best tutorial for dynamic programming on internet.
    Thank you sir for clearing the basics so well

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

      tabulation method me initialization kaise karein?
      please anyone explain the logic!
      Thanks.

    • @RaviKant-nq8xk
      @RaviKant-nq8xk Рік тому

      @@abdulnafe6442 You need to see the recursive logic to identify that when you don't have any item (n in recursion is same as i in tabulation ), then 0th row takes value of 0. similarly if the capacity is zero ( w in recursion is same as j in tabulation) then 0th column will take value of 0. Do watch the knapsack videos(recursion, top down) to understand the logic here in unbounded knapsack.

  • @sujeetkumar.
    @sujeetkumar. 3 роки тому +51

    This question was asked to me this year in TCS exam. I was unable to solve it 😅. Now I can solve it easily. Thank You for awesome teaching.

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

      digital or technical round?

    • @sujeetkumar.
      @sujeetkumar. 3 роки тому +3

      @@sougatoghosh8467 technical round

    • @prakaaashh
      @prakaaashh 3 роки тому +6

      @@sougatoghosh8467 bhai itna to tum bhi soch skte ho , digital round mein DP ka questions to nhi puchhta bro

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

      reallly?
      tcs asks DP?

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

      @@Shourya_performs The TCS exam was for 9 lakh ctc, not 3.5 lakhs bro.

  • @sulekhakumari-hs4gy
    @sulekhakumari-hs4gy 4 роки тому +88

    UNBOUNDED KNAPSACK == ROD CUTTING PROBLEM. GANGADHAR HI SHAKTIMAN H ..

  • @reenayadav7013
    @reenayadav7013 4 роки тому +13

    I haven't finished all videos but already a fan sir! 😇Thank you for teaching these topics .Keep making CP less scary, you are such a saviour 🙌🏻

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

    You're the one who reminds me of the student who teaches us one night before the exam.
    Thanks a lot for the videos.
    You're truly amazing.

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

      tabulation method me initialization kaise karein?
      please explain the logic!
      Thanks.

  • @prashantdubey4112
    @prashantdubey4112 3 роки тому +8

    Aditya bhai!!! Do din se lagataar aapke videos dekh raha hoon!!! aaj se pehle coding seekhne mein itna maza nahi aaya kabhi!! Thanks to you man!!

  • @Ji-yoon
    @Ji-yoon 3 роки тому +173

    Why are you not making videos nowadays?..... It's a humble request that if you have time please make a playlist on backtracking

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

      Start watching striver's videos

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

      @@parthsalat He is not as good as this guy.

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

      @@humbleguy9891 I agree, but there's no other option

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

      bro,he is roughy making 16 lakhs from his patreon alone each month,I don't think he is coming back to youtube anytime soon

  • @sanjanamaheshwari2178
    @sanjanamaheshwari2178 4 роки тому +8

    Sir you are great! I use to figure out such similarities in questions , but used to think they were useless. Super confident after watching your content.

  • @sanketoreken2980
    @sanketoreken2980 3 роки тому +6

    Love how you repeat the explanations. Thank you for doing this for free

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

      tabulation method me initialization kaise karein?
      please anyone explain the logic!
      Thanks.

  • @jayshree7574
    @jayshree7574 4 роки тому +140

    pls make a playlist on graph too, maybe that will be less scary then, as dp is now

  • @tanyamehrotra8832
    @tanyamehrotra8832 4 роки тому +37

    Thank you for existing.

  • @msingla135
    @msingla135 Рік тому +5

    Great explanation Aditya. Only 1 feedback, it'd be even better if you can do a complete dry run of 1 testcase atleast. It'll help us visualie the scenario. Drawing parallels is fine but not so easy. Like in this video, it is not clear that why your formula works!

  • @himanshugupta7010
    @himanshugupta7010 3 роки тому +3

    bow down to aditya verma .. the way he teaches is just out of universe . no one teaches so good brother . Thank you so much for making my dp stronger than ever before.

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

      tabulation method me initialization kaise karein?
      please anyone explain the logic!
      Thanks.

  • @PragatiGupta-e7l
    @PragatiGupta-e7l 11 місяців тому +1

    A big thankyou for this DP playlist Aditya sir. You made the life easy:)
    Jut one thing to add for this question:
    When i=0 and j !=0 i.e. we don't have rod of any length to be picked up(i=0), but there is still some length remaining(j !=0), in that case the path is not feasible. Therefore, we should initialize it with Integer.MIN_VALUE instead of 0.
    Here is the code in java:
    int dp[][] = new int[n+1][n+1];
    for(int i=0; i

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

    Within 7 minutes I was able to stop the video and make a successful submission. Enjoying the binge watch and solving. Pausing and thanking you for it. Aur kaafi bacha hai.

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

    DP was so scary to me until your videos came to rescue!

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

    still people watching your videos on you tube big bro....you are the real master of dsa...your way of teaching is masterpiece...no is like you.
    🤟🤟🤟🤟

  • @umangagrawal8944
    @umangagrawal8944 4 роки тому +17

    You are great Sir! Good time going in these days just because of your videos. DP is love and you made it loveable man!!:)

    • @deeproy7292
      @deeproy7292 4 роки тому

      bhai jb sum ban jate hai toh accha lagta hai...confidence aata hai ki merese bhi solve ho sakta hai

  • @Udzial
    @Udzial 3 роки тому +1

    Awesome just a small change i.e. t[i] instead of t[i-1] did the whole magic
    Thank you for relating the things. which makes it easy to remember and understand things

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

    this playlist is very useful for me , thank you sir 😊😊.

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

    13:12 😅😂🤣bahut prachin problem hai. Next level explaination bro

  • @mohamedfaizan3095
    @mohamedfaizan3095 3 роки тому

    Sir you are fabulous i never saw anyone explaining problems like you

  • @sumekagarwal8229
    @sumekagarwal8229 4 роки тому +1

    Oooooohhh bhai.......Angaar lagaa rahe ho tusi! Bus ek chotah se request, 0/1 aur unbounded ke jitne bhi problems hai unke links apne description pe daldena, practice karne ko easy hojata, Buss us question ka nahi, koi aur hatke vaale

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

    bestest way of teaching

  • @priyarathore9266
    @priyarathore9266 3 роки тому +1

    Best DP course on internet!

  • @mrunaligaikwad1953
    @mrunaligaikwad1953 3 роки тому +4

    The explaination did not clear how to identify an unbounded knapsack, only thing i could get is that in an unbounded knapsack, the elements can be repeated. please elaborate how would we know to solve a problem with unbounded knapsack as a base?
    Will it be mentioned in the question that repetition is allowed?

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

      Yes, I was also thinking how to identify whether a knapsack has to be solved as 0-1 or unbounded, and Aditya didn’t gave any hint about this. According to me, in this question it is mandatory that the length of all the segments has to be equal to the length of the rod (that is N). if we’ll try solving this using 0-1 knapsack, we might not be able to get this condition fulfilled, that is in case of 0-1, there’s a possibility that the length of all the segments maybe or may not be equal to the length of the rod, because in case of 0-1 it can also come out to be less than the length of the rod, which we have seen in 0-1 knapsack problems, that the knapsack is not fully filled sometimes.
      In this problem the length of the rod must be equal to the sum of length of all the segments, or you can say that it is a kind of knapsack problem with an extra condition that the knapsack has to be filled completely upto its capacity. By using an element many times we might be able to completely fill the capacity, which is sometimes not possible with 0-1. So try using unbounded in cases where you have to completely fill or where you have to reach the capacity or length or anything given in the problem.
      And it is not mentioned in the question, if you'll check on various platforms, its not mentioned there about repetition and all.

  • @charan775
    @charan775 4 роки тому +47

    I think the code is wrong. In knapsack the total weight of selected items need not be equal to Knapsack capacity. But in this case the total length of selected segments must be equal to Length of the rod not less.

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +22

      but that doesnt make our code wrong?! that just makes it to be a case where we can fully fill the knapsack always.

    • @charan775
      @charan775 4 роки тому +4

      @@TheAdityaVerma let's say state is (n, length). Say we are finding (1,9). First element length is also 9 then (1,9) = 1+ (0,0) = 1 and it's correct. But if length is is 7.
      (1,9) = 1 + (0,2) and since we have initialised both 1st row & 1st col as 0, (1,9) = 1 but that's wrong since we only have 7 in our array and can't make 9 length

    • @charan775
      @charan775 4 роки тому +1

      changing the base case is working

    • @joshiadvait8
      @joshiadvait8 4 роки тому

      @@charan775 why 1+....

    • @charan775
      @charan775 4 роки тому +1

      @@joshiadvait8 because you include a segment

  • @prernasurbhi9681
    @prernasurbhi9681 3 роки тому

    One of the best explanation ever found , thank you so much

  • @vladimirputin2756
    @vladimirputin2756 Рік тому +12

    public class rodCutting {
    public static void main(String[] args) {
    // Define the profits and lengths for each possible rod piece.
    int price[] = { 1, 5, 8, 9, 10, 17, 17, 20 }; // Profits associated with rod pieces.
    int length[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; // Lengths of corresponding rod pieces.
    int n = 8; // Total length of the rod.
    // Create a 2D array 'dp' for dynamic programming.
    int[][] dp = new int[price.length + 1][n + 1];
    // Initialize the first row (no rod pieces) and first column (no length) of 'dp'
    // to 0.
    for (int i = 0; i < price.length + 1; i++) {
    for (int j = 0; j < n + 1; j++) {
    if (i == 0 || j == 0) {
    dp[i][j] = 0;
    }
    }
    }
    // Fill in the 'dp' array to compute the maximum profit using dynamic
    // programming.
    for (int i = 1; i < price.length + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
    if (length[i - 1]

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

    Your videos are great. But here i guess it would be better if you emphasized more on approaching the problem from the rod cutting part instead of directly trying to build the code by making the values analogous to the previous question. I.e some more insight into the real problem would be great. By the way great work 👍

  • @VipinKumar-us1sr
    @VipinKumar-us1sr 3 роки тому +2

    instead of taking length array we can take a variable len = n. and replace len[i-1] with i, where 0

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

      can u share code?

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

      @@kapilsingh2816 class Solution{
      public:
      int cutRod(int price[], int n) {
      //code here
      // int length[n+1];
      // for(int i=0;i

  • @aanchalsharma5264
    @aanchalsharma5264 4 роки тому +7

    u can even make a paid course .....but please make playlist on competitive programming
    it is difficult and we want teacher like you
    please brother

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

    great videos !! please also make one video giving us a road map on how to do extra maths required for higher level problems

  • @namekuldeep
    @namekuldeep 4 роки тому +1

    Very Good Aditya.. really impressed with your hard work .. I know it takes lot of time to make this .. and help everyone .. Really DP looks very easy now.

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

    Thank you very much. You are a genius.

  • @charan7240
    @charan7240 4 роки тому +6

    can u say recurse code for these dp problems cause mostly u're going through tabulation method.

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

    please bro more videos on dp in trees and graph from leetcode hard questions. Fabulous Work!!! 🤩👏

  • @AnkushKumar-mk8ns
    @AnkushKumar-mk8ns 4 роки тому +4

    can we do this problem with recursion + memoization similar to 0 1 knapsack

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

    Kitna achha padate ho bhaiya aap.. Please greedy bit manipulation or graph ke liye bhi playlist bnado

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

    Great content. Please make videos on graphs and trees. Please.

  • @manavshah7450
    @manavshah7450 3 роки тому

    i love the way you rotate the pen with our fingers :-))
    PS: Amazing Explanation.

  • @pradeepdeshmukh843
    @pradeepdeshmukh843 4 роки тому

    Your explanations are very much easy to understand.
    If possible, can you please make video on choice diagram of rod cutting problem.

  • @anmoldubey4952
    @anmoldubey4952 3 роки тому +5

    12:54
    "Ah, I see you're a man of culture as well"

  • @codeb3nder862
    @codeb3nder862 4 роки тому +22

    Vs code shortcut ctrl+H replace weight to length, value to price

  • @ashishkhar4791
    @ashishkhar4791 3 роки тому

    bhai yr bhot sahi kaam kia hai apne agr aap greedy ya back tracking ki bhi aasi playlist bnado bhot help hogi

  • @ujjwalsotra7607
    @ujjwalsotra7607 3 роки тому

    The way he relates everything is just awesome😎😎

  • @anshulbansal8828
    @anshulbansal8828 4 роки тому

    is problem ko sochne ka najariya hi badal dia aapne,,, thankz

  • @akshaydeshpande127
    @akshaydeshpande127 4 роки тому +22

    0-1/Fractional/Unbounded knapsack could be filled without reaching the capacity, but in rod cutting you need to reach the capacity (total length). How do you tackle this? @aditya

    • @0anant0
      @0anant0 4 роки тому +3

      When you make a cut, u choose the price for that len. The remaining part of the rod is also optimally cut. You sum up these prices -- cur max price = curr len price + 'max' price for remaining len

    • @gokulgopinath3777
      @gokulgopinath3777 4 роки тому +11

      initialize 0th row by minus infinity(-inf).

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

      @@gokulgopinath3777 Yes it works, but can you explain why??

    • @Kaivalyamani
      @Kaivalyamani 3 роки тому +1

      @@VIVEK1898 if u dont know the array of price and length and rod is given to u then how u can find profit if u dont know the price distribution

    • @Animax590
      @Animax590 3 роки тому

      @@Kaivalyamani yeh for maximum loss it is -inf

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

    This is really amazing .....Great explanation

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

    4:34 waah kya style hai Pen change karne ka :D :D ;)

  • @noobichesser9434
    @noobichesser9434 3 роки тому +3

    Engrave this in stones and history will witness that best ever tutorials created for Dynamic Programming on entire planet was created by Aditya Verma. Period.

  • @ankurgupta9068
    @ankurgupta9068 3 роки тому +1

    If only price array and length as an argument is given then what would be its recursive code without memoization?

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

    In given problem link many are assigning t[0][i]=INT_MIN
    Isn't we are talking about length here, how can we take loss?? I didn't get this, ig minimum value can be '0' only, right? then why INT_MIN?

    • @LearningYouth
      @LearningYouth 3 роки тому +1

      Same question!! Did you figure out later? Aditya's approached worked for me for the given link. I don't understand what's happening here.

    • @atharvacodes3705
      @atharvacodes3705 3 роки тому

      @@LearningYouth same here. Tell me if you figure it out.

  • @ShrutiAgrahari0803
    @ShrutiAgrahari0803 3 роки тому

    Best explanation sir !! this video is really really helpful sir !!

  • @HimanshuSingh-yh8lz
    @HimanshuSingh-yh8lz 3 роки тому +1

    please post the link of similar problems from coding various websites, it will be for understanding the topic...BTW your videos are among the best

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

    U deserve a million of blessings

  • @AjayKumar-ww1vp
    @AjayKumar-ww1vp Рік тому

    bhaiya yrr ekdum bawaal chiz hai 🔥...ekdum gardaa uda diye😅❤❤

  • @rahulsisondia2606
    @rahulsisondia2606 4 роки тому +5

    Recursive version. You can do memoization easily.
    int cutting_rod(vector price, int length, int cut) {
    if (length

  • @ShagunSharma-c6t
    @ShagunSharma-c6t Рік тому

    what is the difference between size and length? ( during the end when we make table ....for that)

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

    I first thought of a 0/1 knapsack kinda approach where each "n" is --> do you want to cut here or not?
    if cut here, store the value, and pass to recursive call (n-1, 0)
    if not cut here, increment the value, and pass to recursive call(n-1, value + arr[n-1])
    for each n, return max of these 2 values
    but that was the wrong approach. though I conceptually didn't get why it was the wrong way...

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

    Given the stock bars of length L=12m (plenty in stock), Lengths to cut = Array(3.5, 4.2, 6.5, 5.7)m, Quantity required = Array(50, 30, 70, 45) nos. To find the minimum number of stock bars required. How to do this? Please advice.

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

    U R THE RESON I DEVELOPED INTEREST IN DSA

  • @mohitmajithia2687
    @mohitmajithia2687 4 роки тому +1

    in rod cutting how it i related? in unbound we can take less than wt? but in rod can we take as cut less than length?

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

    Epic teaching bhai!!!

  • @coldfinger3472
    @coldfinger3472 3 роки тому +7

    " Unbounded Knapsack ek pracheen problem hai " - Aditya Verma [2020].

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

    What does a cell here represent in the table? This one seems confusing. Each column represents length of rope and each row represents what?? Can anyone explain exactly what is the cell at location i,j represents and what is i and what is j there precisely??

    • @kokilgupta1750
      @kokilgupta1750 4 роки тому

      Each row represents the length of rod and column represents the size of array . So if we are at dp[i][j], it means that this cell gives solution when the length of rod is j and size of array is i.
      For example if array={5,5,2} and length = 7 then the cell dp[3][2] represents a situation in which length of rod is 3 and size of array is 2 i.e elements of array available for this length are {5,5}

    • @kokilgupta1750
      @kokilgupta1750 4 роки тому

      This is just a general idea of what dp represents for cell [i][j]

  • @rishikeshkumar2708
    @rishikeshkumar2708 4 роки тому +6

    Sir can you explain the cut ribbon problem of codeforces using this technique,I tried to use but it failed

    • @vaishalithakur1212
      @vaishalithakur1212 4 роки тому

      Yeah i'm also stuck with same problem.

    • @vaibhavtiwari6540
      @vaibhavtiwari6540 3 роки тому

      He has done the initialization wrong, that is why; And did not correct it yet, like he does not care. Initialise row 0 with -inf.

  • @gurpartapsingh1693
    @gurpartapsingh1693 4 роки тому +6

    Sir, on the link that you have mentioned, they have provided all the solutions using O(n) extra space but we are using O(size*N). How can we optimize it to O(N)?

    • @Praveen-zq8ox
      @Praveen-zq8ox 3 роки тому +1

      Just use a for loop for length array and an array for storing instead of matrix

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

      yes , it is mentioned that we have to use O(N) auxiliary space, but gfg has accepted my solution even though i've used a matrix. How is this happening?

  • @dipakraut6058
    @dipakraut6058 4 роки тому

    Amazing work buddy🙌🙌

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

    what is minimize, is that same by replaceing max with min

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

    Minimum cost to cut a stick on leetcode is not unbounded knapsack??

  • @ankit_yadav11
    @ankit_yadav11 8 місяців тому

    i successfully build the recursive tree my my own but i m confused abut LC hard problem min coast to cut rod is quite different

  • @VS-rc4fs
    @VS-rc4fs 5 місяців тому

    in this code how we will write the intialisation? how we will fill the table??

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

    but if the size of data, length>=1e4 than the algo will not provide any result because of large dataset. How can we fix it?

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

    There is no need to make an extra Length array, simply traversing from 1 to n in for loop will do the task.

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

    We can solve unbounded knapsack related questions using 1D array too. No need to store the information in 2D array; reduces space complexity.

  • @panavkumar9009
    @panavkumar9009 3 роки тому

    Can someone post the complete solution of the question given in the description.

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

    how Time can be Linear ??? May be you have mistaken with space because GFG uses 1D array(No need to keep track of items that was inserted or not). and he is using 2D array(Keeping track of items which is redundant). All Unbounded Knapsack problems can be solved by using 1D array. He is maintaining the similarity for viewers so he is not confusing people by trying to give the most optimal approach where solution may differ from one another.

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

    Sir please make a playlist on graph.

  • @pirtpalsingh8804
    @pirtpalsingh8804 4 роки тому +1

    In initializing the matrix when size of length array is zero ,then why we are not getting profit equal to total length. And we are initializing first column and row with 0.

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

    your analogies are damn good 😍😍

  • @nishantagrawal6244
    @nishantagrawal6244 3 роки тому +1

    Great as always Aditya :) But the code in this video is missing a case that sum of lengths of selected segments of rod must be equal to the total length of the rod. This case can be handled by changing the initialization of dp matrix as mentioned below.
    for(int i=0; i

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

      how does INT_MIN help with that??? i have trouble visualizing this

    • @gautamsuthar4143
      @gautamsuthar4143 3 роки тому

      Why you initialised the first row with INT_MIN?

  • @spritech525
    @spritech525 4 роки тому

    kya tarika hai yar padhane ka :) .. amazing

  • @hemadhariwal9847
    @hemadhariwal9847 3 роки тому

    u r amazing man, m in love with dp!

  • @codestorywithMIK
    @codestorywithMIK 4 роки тому +5

    Hi @Aditya Verma and everyone, I have a small confusion and need some help :
    There is this question in which array of pieces(size) to be cut from a rod(or ribbon or line segment) is given. and maximum length of rod(or ribbon or line segment) is given. We have to find the maximum number of segments we can make by cutting only of the sizes given.
    For example :
    max size of rod = 7
    pieces allowed to cut = [5, 2, 2] (n = 3 , i.e. size of this array is 3)
    Correct Output = 2 (i.e. we can get "two" segments i.e. 5 and 2)
    I tried solving it using the approach taught above, i get 3 as my answer and I know somewhere I am going wrong. In case you guys want to see my approach, this is a small segment which explains all : (initialization has been done)
    for(int i = 1; i

    • @jimenluhar7126
      @jimenluhar7126 4 роки тому +7

      def fun(a,n):
      dp=[[0 for i in range(n+1)] for j in range(4)]
      for i in range(1,4):
      for j in range(1,n+1):
      if j>=a[i-1]:
      if dp[i][j-a[i-1]]!=0 or j==a[i-1]:
      dp[i][j]=max(dp[i-1][j],1+dp[i][j-a[i-1]])
      else:
      dp[i][j]=dp[i-1][j]
      else:
      dp[i][j]=dp[i-1][j]
      return dp[-1][-1]
      refer this ....,your code is correct but you only have to add one condition (my 6th line).....kyunki agar apan rod ko divide kar rahe hai toh uske saare parts (elements) given array(pieces) mai se hi hone chahiye

    • @codestorywithMIK
      @codestorywithMIK 4 роки тому +1

      @@jimenluhar7126 Thanks a lot brother. Just one thing, I can understand "j == a[i-1]" which means that "piece ka size" should be in the array. But iska matlab kya hua "dp[i][j-a[i-1]]!=0" ?? Can you please explain this ? I will be very thankful

    • @jimenluhar7126
      @jimenluhar7126 4 роки тому +1

      @@codestorywithMIK agr j-a[i-1] not equal to 0 hua... matlab ki apn currently jis piece k liye loop chala rahe h uske bina bhi apni rod valid h (mtlb agar puri rod mai se apan ne a[i-1] element nikal diya toh jo rod bachi hai uski length prices array mai honi chahiye ya fir usko further break kiya ja sakta h !!)

    • @jimenluhar7126
      @jimenluhar7126 4 роки тому +1

      @@codestorywithMIK for example n=7
      Pieces=2,2,5
      Toh tumhare code k hisaab se 3 cut hone chaiye jisse 3 elements ki length 2 hogi aur aik ki 1 (2+2+2+1=7) but jo rod ka element h uski length 1 nahi ho sakti kyunki woh pieces array mai nahi h !! mtlb correct answer 2 cut h (2+5) jo valid hai

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

      @@codestorywithMIK agar dp[i-1][j-a[i-1]] equal to 0 hua matlab ki j-a[i-1]. ko apan valid nahi bol sakte kyunki woh pieces array mai bhi nahi hoga aur usko pieces array k elements use karke bhi break nahi kiya ja sakta (use element ko apan ne pehle hi process kar liye h ).

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

    can we solve it using partition DP ?

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

    Solved at 3:00, Thanks Man

  • @sawanpandita8352
    @sawanpandita8352 4 роки тому +1

    @Aditya Verma What are the sources from where we can have hands-on on this topic.Your videos are worth watching but having a hands-on would surely clear all the doubts i guess!

    • @mayankchaudhary5515
      @mayankchaudhary5515 4 роки тому +1

      do practice at geeksforgeeks portal, all questions are available there

    • @rajvijen
      @rajvijen 4 роки тому +1

      atcoder.jp/contests/dp/tasks this is a complete list of DP problems, here you can practice more. (I think after watching Aditya's videos you can do all of them ).

  • @BiharCentralSchool
    @BiharCentralSchool 3 роки тому

    Same to Same question was asked in Cognizant GenC Next Online Coding Round.

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

    Please suggest some good playlist/channel for
    1. Graph
    2. Mathematics questions

  • @Rahulverma__
    @Rahulverma__ 3 роки тому +1

    A good teacher but sir thoda try to make video faster 1 baat again and again

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

    i did it the same way still wrong answers are coming every time. can you pls help

  • @ankuragarwal4014
    @ankuragarwal4014 4 роки тому +6

    please explain how to reduce space complexity from O(n2) to O(n)

    • @TheAdityaVerma
      @TheAdityaVerma  4 роки тому +10

      It's really hard to explain anything over text, will try to make a video, till then you can find articles about it. Thanks for watching brother !!

  • @pruthvirajk6019
    @pruthvirajk6019 4 роки тому +4

    can i get the notes of urs ?? Softcopy///pdf///

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

    nice explanation

  • @vikasvk9174
    @vikasvk9174 4 роки тому

    Waiting for bhai ke 30k subscribers hone ka :)