Rod Cutting | Practice | GeeksforGeeks

Given a rod of length N inches and an array of prices, price[] that contains prices of all pieces of size smaller than N. Determine the maximum value obtainable by cutting up the rod and selling the pieces.
 
Example 1:
Input:
N = 8
Price[] = {1, 5, 8, 9, 10, 17, 17, 20}
Output:
22
Explanation:
The maximum obtainable value is 22 by
cutting in two pieces of lengths 2 and 
6, i.e., 5+17=22.

Example 2:
Input:
N=8
Price[] = {3, 5, 8, 9, 10, 17, 17, 20}
Output: 24
Explanation: 
The maximum obtainable value is 
24 by cutting the rod into 8 pieces 
of length 1, i.e, 8*3=24. 

Your Task:  
You don't need to read input or print anything. Your task is to complete the function cutRod() which takes the array A[] and its size N as inputs and returns the maximum price obtainable.

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 1000
1 ≤ Ai ≤ 105
+1
varun32545 minutes ago
Space optimized bottom up dp: 
Time: O(n)^2
Space: O(n)
 
class Solution{
   public int cutRod(int price[], int n) {
       //code here
       
       int dp [] = new int[n+1];
       
       for(int i=1;i<n+1;i++){
           for(int j=1;j<n+1;j++){
               
               if(i<=j){
                   dp[j]= Math.max(dp[j],dp[j-i] + price[i-1]);
               }
               
           }
       }
       
       return dp[n];
   }
}
 
0
chessnoobdj10 hours ago
C++ recursive
int f(int price[], int idx, int n){
    if(n <= 0 || idx > n)
        return 0;
    int a = price[idx-1]+f(price, idx, n-idx);
    int b = f(price, idx+1, n);
	return max(a, b);
}
int cutRod(int price[], int n) {
    return f(price, 1, n);
}
C++ memoization
int dp[1001][1001];
int f(int price[], int idx, int n){
    if(n <= 0 || idx > n)
        return 0;
    if(dp[idx][n] != -1)
        return dp[idx][n];
    int a = price[idx-1]+f(price, idx, n-idx);
    int b = f(price, idx+1, n);
    return dp[idx][n] = max(a, b);
}
int cutRod(int price[], int n) {
    memset(dp, -1, sizeof(dp));
    return f(price, 1, n);
}
C++ DP
int cutRod(int price[], int n) {
    int dp[n+1][n+1];
    memset(dp, 0, sizeof(dp));
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            dp[i][j] = dp[i-1][j];
            if(j-i >= 0)
               dp[i][j] = max(
               dp[i][j], price[i-1]+dp[i][j-i]);
        }
    }
    return dp[n][n];
}
C++ DP optimized
int cutRod(int price[], int n) {
    int dp[n+1] = {0};
    for(int i=1; i<=n; i++)
        for(int j=0; j<i; j++)
            dp[i] = max(dp[i], price[i-j-1]+dp[j]);
    return dp[n];
}
0
aamitprasad61815 hours ago
CPP 
 
int length[n];
        for(int i=0;i<n;i++){
            length[i]=i+1;
        }
        
        int dp[n+1][n+1];
        
        for(int i=0;i<=n;i++){
            dp[i][0]=0;
        }
        for(int j=1;j<=n;j++){
            dp[0][j]=0;
        }
        
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                
                if(length[i-1] <= j){
                  dp[i][j]=max( (price[i-1] + dp[i][j-length[i-1]]) ,dp[i-1][j] );  
                }
                else{
                    dp[i][j]=dp[i-1][j];
                }
            }
        }
        
        
        return dp[n][n];
0
younusosama2 days ago
For the first example why can't we pick 3 and 5. It would give us a greater price
0
anirudh17thebest3 days ago
 TOP-DOWN DP JAVA sol
class Solution{
   
   public int helper(int arr[],int price[],int length,int i,int t[][]){
       if(i==0 || length==0) return 0;
       if(t[i][length]!=-1) return t[i][length];
       if(arr[i-1]>length) return helper(arr,price,length,i-1,t);
       int op1=0+helper(arr,price,length,i-1,t);
       int op2=price[i-1]+helper(arr,price,length-arr[i-1],i,t);
       return t[i][length]=Math.max(op1,op2);
   }
   public int cutRod(int price[], int n) {
       //code here
       int arr[]=new int[n];
       for(int i=0;i<arr.length;i++){
           arr[i]=i+1;
       }
       int t[][]=new int[n+1][n+1];
       for(int i=0;i<t.length;i++){
           for(int j=0;j<t[0].length;j++){
               t[i][j]=-1;
           }
       }
       return helper(arr,price,n,price.length,t);
   }
}
-1
mirajhc2vv4 days ago
using memoization 
   int t[1000][1000];   int cutRod(int price[], int s, int n=-33) {       if(n==-33){n=s; memset(t, -1, sizeof(t));}       if(s==0 || n==0){t[n][s]=0; return 0;}              if(t[n][s]!=-1)           return t[n][s];                  if(s>=n){           return t[n][s]=max(cutRod(price, s-n, n)+price[n-1], cutRod(price, s, n-1));       }       else{           return t[n][s]=cutRod(price, s, n-1);       }          }
0
prince910011 week ago
JAVA Solution (DP: Tabulation) | Essentially A Variation Of Coin Change Problem, But Here We Need To Define The Parameter:
 
public int cutRod(int price[], int n) {
        //code here
        
        int dp[][] = new int[n+1][n+1];
        int lengthOfRod[] = new int[n];
        
        for(int i = 0; i<n; i++)
        {
            lengthOfRod[i] = i+1;
        }
        
        
        //Base Case
        
        for(int i = 0; i<=n;i++)
        {
            for(int j = 0; j<=n; j++)
            {
                if(i == 0 || j == 0)
                dp[i][j] = 0;
            }
        }
        
        for(int i=1;i<=n;i++)
        {
            for(int j=1; j<=n; j++)
            {
                
                int not_pick = 0+ dp[i-1][j];
                int pick = Integer.MIN_VALUE;
                
                if(lengthOfRod[i-1]<=j)
                {
                    pick = price[i-1] + dp[i][j-lengthOfRod[i-1]];
                }
                
                dp[i][j] = Math.max(pick,not_pick);
            }
        }
        
        return dp[n][n];
    }
0
sanyambanoni11 week ago
#cpp c++  memoization
TOP down memoization using 1d array→
class Solution{ public:   int solve(int price[],int n,int len,vector<int>&dp)   {       if(len==0)       {           return 0;       }       if(dp[len]!=-1)       {           return dp[len];       }       int res=INT_MIN;       for(int i=1;i<=len;i++)       {       int  ans=  solve(price,n,len-i,dp)+price[i-1];       //we are reduccing len bcoz each time our rod length also decreasing       res=max(res,ans);       }       dp[len]=res;       return dp[len];   }   int cutRod(int price[], int n) {       //code here        vector<int>dp(n+1,-1);     return solve(price,n,n,dp);
}}
0
divyanshpandey033961 week ago
int cutRod(int price[], int n) {       //code here       int size=sizeof(price);       size=size-1;       int dp[size+1][n+1];       for(int i=0;i<=size;i++) dp[i][0]=0;       for(int j=0;j<=n;j++) dp[0][j]=0;       for(int i=1;i<=size;i++)       for(int j=1;j<=n;j++){           if(i<=j) dp[i][j]=max(price[i-1]+dp[i][j-i],dp[i-1][j]);           else dp[i][j]=dp[i-1][j];       }       return dp[size][n];   }
-2
parasdhanwal1 week ago
Java | DP | Memoization
 
class Solution {
    public int minCost(int W, int[] cuts) {
        Arrays.sort(cuts);
        int N=cuts.length;
        int dp[][]=new int [N][W+1];
        for(int rows[]:dp)
        Arrays.fill(rows,-1);
        
        return knapSack(N-1,W,cuts,dp);
    }
    
    static int knapSack(int ind, int W, int val[],int [][]dp)
    {
        if(ind==0)
        {
            int rodlen=ind+1;
            int ans=W-rodlen>=0?(int)((W/rodlen)*val[ind]):0;
            return ans;
        }
        
        if(dp[ind][W]!=-1) return dp[ind][W];
        
        int nt=knapSack(ind-1,W,val,dp);
        int t=Integer.MAX_VALUE;
        int rodlen=ind+1;
        if(W-rodlen>=0)
        t=val[ind]+knapSack(ind,W-rodlen,val,dp);
        
        return dp[ind][W]=Math.min(nt,t);
    }
}
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.