Rod Cutting Problem | Dynamic Programming | Unbounded Knapsack

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024

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

  • @kr_ankit123
    @kr_ankit123 3 года назад +11

    Here is my tabulation approach for this problem. Some may find it easier if they would have gone through the previous videos in this playlist. Here rod_len is as same as that of capacity in knapsack and n is the size of the length array. Here one thing is to note that if length array is like {1,2,4,5,7,9}(it means we can cut array in pieces of lengths 1,2,4,5,7,9) and rod_length is 9 then n value will be 6 not 9.
    int rod_cutting(int price[],int length[],int rod_len,int n){
    int dp[n+1][rod_len+1];
    for(int i=0;i

  • @therohitpatil
    @therohitpatil 3 года назад +12

    You have got the skill to explain the concept very well. Thank you for doing this. Also, which software do you use for this kind of presentation?

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

    Mate where have you been for God sake? I've stuck on this problem for long time. Many thanks

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

    Awesome way of Teaching . Got the concept very easily . Thank You So Much for providing us with valuable content 😍

  • @gautamsuthar4143
    @gautamsuthar4143 3 года назад +4

    07:14 Sir if we take i from 1 to N then it should be price[i-1]+CutRod(N-i). because price[n] is not defined.

  • @ashishkhoiwal9330
    @ashishkhoiwal9330 3 года назад +5

    @TECH DOSE , sir, In code why are you not checking if the subproblem is already solved (that's the main purpose of memoisation).
    you are just storing it in mem but not using it except for the end (return mem[N][maxLen]). Why?

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

      I think it's a typo, but the concept was referred to the same If you refer to his (0/1) explanation.

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

    My alternative recursive solution without using the for loop:
    int maxProfitRodCutting (vector length, vector profit, int n, int target)
    {
    if (target

  • @sainikhil956
    @sainikhil956 3 года назад +1

    Sir ! i feel like there is a lot of similarity to coin change and this problem like
    coins=cuts
    target=max_len
    n=n
    but here we are just maximizing the profit ???

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

    What is the difference between N and maxlen. N is length of rod, so it should be 4, what is maxlen?

  • @dayanandraut5660
    @dayanandraut5660 3 года назад +1

    Dear techdose, all the solutions explained so far takes O(N*W) space. How can we reduce it to O(W) space? Thanks in advance

    • @techdose4u
      @techdose4u  3 года назад +1

      See if you need only the previous state then you can maintain 1D array and do it

  • @diptiranjandash8877
    @diptiranjandash8877 3 года назад

    I am just giving a try to tabulation approach -- not sure what's wrong.
    def rodCutting(price, W, N):
    DP = [[0 for x in range(W+1)] for j in range(N+1)]

    for i in range(0, N+1):
    DP[i][0] = 1
    for i in range(1, N+1):
    for j in range(1, W+1):
    if j < price[i-1]:
    DP[i][j] = DP[i-1][j]
    else:
    DP[i][j] = max(DP[i-1][j],price[i-1]+DP[i][j-price[i-1]])
    return DP[-1][-1]

    • @saptarshidas488
      @saptarshidas488 3 года назад

      In the DP table, the sub problems where we evaluate for rod length = 0, how can any profit be realized? There is no rod to cut. The code snippet :-
      for i in range(0,N+1):
      DP[i][0] = 1
      Change that 1 to 0.
      Also here, within the inner for loop, else condition: the case where we cut the rod by the given length, we have to do dp[i][j-length[i-1]] and not dp[i][j-prices[i-1]]

  • @Usurperhk
    @Usurperhk 3 года назад +1

    Awesome explanation, great job TECH DOSE!

  • @RTX_valorant
    @RTX_valorant 3 года назад +1

    Bro it's same as 0)1 bounded knap sack right? What's the difference?

    • @techdose4u
      @techdose4u  3 года назад +1

      It is Unbounded knapsack. Already explained why in the video. Also explained in one of the comment. Please watch it.

  • @mykolapetriv2017
    @mykolapetriv2017 3 года назад

    Thank you for the amazing work

  • @E__ShameemahamedS-bx2ed
    @E__ShameemahamedS-bx2ed 3 года назад +1

    I think it's not unbounded knapsack because in the recursive solution an instance is excluded or included 01

    • @techdose4u
      @techdose4u  3 года назад +1

      01 property is always present for all types of integer knapsack. 01 knapsack will mean that you can't have 2 sub-rods of the same length which is not true. You can have unlimited number of sub-rods of the same length as long as the rod doesn't finish. It's not bounded knapsack because we don't have any hard limit on number of sub-rods of a given length. I hope you got it.

  • @priestj3125
    @priestj3125 3 года назад +3

    Hi Surya.pratap.k, Amazing video series. you are awesome!!. can you please explain leet code#698 and relate to this playlist
    so far? what happens if the array has duplicate elements in it and can we still apply the grid approach?

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

      I dint see the question. I will check it once I get some time.

  • @Dankman3
    @Dankman3 3 года назад +1

    Thank you sir

  • @syedkaja2045
    @syedkaja2045 3 года назад +1

    amazing bro!!!

  • @hayyanhami6826
    @hayyanhami6826 3 года назад

    Isn't that p[i+1] ? since the new range is [0,n-1)

  • @syedkaja2045
    @syedkaja2045 3 года назад

    max(maxprofit(profit[],len[],i+1),profit[i]+maxprofit(profit[],len[],i+1))
    will this work ?
    base case
    i==length of rod
    return 0

    • @spetsnaz_2
      @spetsnaz_2 3 года назад

      You also have to pass N, which is the upper bound of the array length, otherwise you will get segmentation fault.

  • @gourav.barkle
    @gourav.barkle 3 года назад

    How maxlen is our maximum capacity, like we want and can have as much price we can?

    • @saptarshidas488
      @saptarshidas488 3 года назад +1

      here, maxlen is the length of the rod. We can cut a rod of length 4 any number of times but cannot cut it for a length greater than 4, haina?

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

      @@saptarshidas488 exactly

  • @E__ShameemAhamedS
    @E__ShameemAhamedS 3 года назад +1

    max len refers to what and n refers to what pls expalin i cant understand

    • @techdose4u
      @techdose4u  3 года назад

      Maxlen is the capacity of bag. Please watch 01 knapsack video using memoization and then come back to understand this. It will be easier.

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

    Shrey sir OP🔥

  • @kshitijgaur9635
    @kshitijgaur9635 3 года назад

    The problem of overlapping sub-problems is not solved. In the mem array, the same result gets recomputed again and again instead of simply returning the previously calculated result. Please explain how is this issue being resolved.

    • @krishjani2103
      @krishjani2103 3 года назад

      I guess it's because he forgot to return the value if it's already present. He's just adding it into the table but not using it when needed.

  • @syedkaja2045
    @syedkaja2045 3 года назад +1

    make video on jump game problem

    • @techdose4u
      @techdose4u  3 года назад

      I think it's made. Check it once.

    • @spetsnaz_2
      @spetsnaz_2 3 года назад

      @@techdose4u already made

  • @pleasesirmorevideos4684
    @pleasesirmorevideos4684 3 года назад +1

    sir i can't find anywhere on internet where i can submit this

    • @techdose4u
      @techdose4u  3 года назад

      This is present in geeksforgeeks and a harder variation is present on interviewbits dp section.

    • @pleasesirmorevideos4684
      @pleasesirmorevideos4684 3 года назад

      @@techdose4u ok sir

  • @NishantSharma-sz5xh
    @NishantSharma-sz5xh 3 года назад

    Ok and ok

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

    seems to complex explanation :l

  • @TomerBenDavid
    @TomerBenDavid 3 года назад +1

    :)

  • @E__ShameemAhamedS
    @E__ShameemAhamedS 3 года назад

    pls reply sir