Rod Cutting Problem | Dynamic Programming | Unbounded Knapsack

  Рет қаралды 64,337

Techdose

Techdose

Күн бұрын

Пікірлер: 55
@kr_ankit123
@kr_ankit123 3 жыл бұрын
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
@saravana6882
@saravana6882 3 жыл бұрын
exactly what i think
@mrboyban
@mrboyban 3 жыл бұрын
Mate where have you been for God sake? I've stuck on this problem for long time. Many thanks
@techdose4u
@techdose4u 3 жыл бұрын
Welcome 😀
@mohammadumaidansari5087
@mohammadumaidansari5087 4 жыл бұрын
Awesome way of Teaching . Got the concept very easily . Thank You So Much for providing us with valuable content 😍
@therohitpatil
@therohitpatil 4 жыл бұрын
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?
@gautamsuthar4143
@gautamsuthar4143 3 жыл бұрын
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 жыл бұрын
@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
@Usurperhk
@Usurperhk 4 жыл бұрын
Awesome explanation, great job TECH DOSE!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@sainikhil956
@sainikhil956 3 жыл бұрын
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 жыл бұрын
What is the difference between N and maxlen. N is length of rod, so it should be 4, what is maxlen?
@diptiranjandash8877
@diptiranjandash8877 4 жыл бұрын
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]]
@mykolapetriv2017
@mykolapetriv2017 4 жыл бұрын
Thank you for the amazing work
@dayanandraut5660
@dayanandraut5660 3 жыл бұрын
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 жыл бұрын
See if you need only the previous state then you can maintain 1D array and do it
@pleasesirmorevideos4684
@pleasesirmorevideos4684 4 жыл бұрын
sir i can't find anywhere on internet where i can submit this
@techdose4u
@techdose4u 4 жыл бұрын
This is present in geeksforgeeks and a harder variation is present on interviewbits dp section.
@pleasesirmorevideos4684
@pleasesirmorevideos4684 4 жыл бұрын
@@techdose4u ok sir
@RTX_valorant
@RTX_valorant 4 жыл бұрын
Bro it's same as 0)1 bounded knap sack right? What's the difference?
@techdose4u
@techdose4u 4 жыл бұрын
It is Unbounded knapsack. Already explained why in the video. Also explained in one of the comment. Please watch it.
@syedkaja2045
@syedkaja2045 4 жыл бұрын
amazing bro!!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@pleasesirmorevideos4684
@pleasesirmorevideos4684 4 жыл бұрын
He is your sir not bro😂
@E__ShameemahamedS-bx2ed
@E__ShameemahamedS-bx2ed 4 жыл бұрын
I think it's not unbounded knapsack because in the recursive solution an instance is excluded or included 01
@techdose4u
@techdose4u 4 жыл бұрын
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 4 жыл бұрын
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 4 жыл бұрын
I dint see the question. I will check it once I get some time.
@E__ShameemAhamedS
@E__ShameemAhamedS 4 жыл бұрын
max len refers to what and n refers to what pls expalin i cant understand
@techdose4u
@techdose4u 4 жыл бұрын
Maxlen is the capacity of bag. Please watch 01 knapsack video using memoization and then come back to understand this. It will be easier.
@Dankman3
@Dankman3 4 жыл бұрын
Thank you sir
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@kshitijgaur9635
@kshitijgaur9635 4 жыл бұрын
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.
@hayyaun
@hayyaun 4 жыл бұрын
Isn't that p[i+1] ? since the new range is [0,n-1)
@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 жыл бұрын
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
@syedkaja2045
@syedkaja2045 4 жыл бұрын
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 4 жыл бұрын
You also have to pass N, which is the upper bound of the array length, otherwise you will get segmentation fault.
@anshuhimanshusuthar5614
@anshuhimanshusuthar5614 3 жыл бұрын
Shrey sir OP🔥
@syedkaja2045
@syedkaja2045 4 жыл бұрын
make video on jump game problem
@techdose4u
@techdose4u 4 жыл бұрын
I think it's made. Check it once.
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
@@techdose4u already made
@NishantSharma-sz5xh
@NishantSharma-sz5xh 3 жыл бұрын
Ok and ok
@shubhamgomase9056
@shubhamgomase9056 Жыл бұрын
seems to complex explanation :l
@TomerBenDavid
@TomerBenDavid 4 жыл бұрын
:)
@techdose4u
@techdose4u 4 жыл бұрын
:)
@E__ShameemAhamedS
@E__ShameemAhamedS 4 жыл бұрын
pls reply sir
@techdose4u
@techdose4u 4 жыл бұрын
Replied
@RTX_valorant
@RTX_valorant 4 жыл бұрын
@@techdose4u lol
Cutting a rod into pieces to maximize Profit (Dynamic Programming)
28:49
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 65 М.
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Grid Game | Leetcode 2017
21:51
Techdose
Рет қаралды 4,9 М.
0/1 Knapsack problem | Dynamic Programming
13:29
WilliamFiset
Рет қаралды 190 М.
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 217 М.
Rod Cutting - Dynamic Programming
15:22
CSBreakdown
Рет қаралды 162 М.
01 Knapsack using Recursion | Building Intuition
18:38
Techdose
Рет қаралды 55 М.
Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges
5:10:02
Dynamic Programming:  the Rod Cutting Problem
9:59
Algorithms with Attitude
Рет қаралды 37 М.
5 Simple Steps for Solving Dynamic Programming Problems
21:27
Reducible
Рет қаралды 1,1 МЛН