Maximize The Cut Segments | Practice | GeeksforGeeks

Given an integer N denoting the Length of a line segment. You need to cut the line segment in such a way that the cut length of a line segment each time is either x , y or z. Here x, y, and z are integers.
After performing all the cut operations, your total number of cut segments must be maximum.
Example 1:
Input:
N = 4
x = 2, y = 1, z = 1
Output: 4
Explanation:Total length is 4, and the cut
lengths are 2, 1 and 1.  We can make
maximum 4 segments each of length 1.

Example 2:
Input:
N = 5
x = 5, y = 3, z = 2
Output: 2
Explanation: Here total length is 5, and
the cut lengths are 5, 3 and 2. We can
make two segments of lengths 3 and 2.
Your Task:
You only need to complete the function maximizeTheCuts() that takes n, x, y, z as parameters and returns max number cuts.
Expected Time Complexity : O(N)
Expected Auxiliary Space: O(N)
Constraints
1 <= N, x, y, z <= 104
0
putyavkain 4 hours
Forward dynamic
int maximizeTheCuts(int n, int x, int y, int z) {
    vector<int> F(n + 1); F[0] = 1;
    int X[] = {x,y,z};
    for (int i = 0; i < n; i++) {
        if (!F[i]) continue;
        for (int j = 0; j < 3; j++)
            if (i + X[j] <= n)
                F[i + X[j]] = max(F[i + X[j]], F[i] + 1);
    }
    return max(0, F[n] - 1);
}
0
moaslam8265 days ago
int fun(int n,int &x,int &y,int &z,vector<int>&dp){
	if(dp[n]!=-1) return dp[n];
	if(n<=0) return dp[n]=0;

    int a=INT_MIN,b=INT_MIN,c=INT_MIN;
	if(n-x>=0){
      a=fun(n-x,x,y,z,dp)+1;
	}
	if(n-y>=0){
      b=fun(n-y,x,y,z,dp)+1;
	}
	if(n-z>=0){
       c=fun(n-z,x,y,z,dp)+1;
	}
	return dp[n]=max(a,max(b,c));
}

    int maximizeTheCuts(int n, int x, int y, int z)
    {
        vector<int> dp(n+2,-1);
        int ans= fun(n,x,y,z,dp);
        if(ans<0) return 0;
        return ans;
    }
0
milindprajapatmst196 days ago
const int N = 1e4;
int dp[N + 1];

class Solution {
    public:
    int maximizeTheCuts(int n, int x, int y, int z, int k = 0) {
        if (n == 0)
            return 0;
        if (n < 0)
            return INT_MIN;
        if (dp[n] == -1)
            dp[n] = max({ maximizeTheCuts(n - x, x, y, z, 1), 
                          maximizeTheCuts(n - y, x, y, z, 1),
                          maximizeTheCuts(n - z, x, y, z, 1) }) + 1;
        if (k == 0)
            return (dp[n] >= 0) ? dp[n] : 0;
        return dp[n];
    }
    Solution() {
        memset(dp, -1, sizeof(dp));
    }
};
0
sanketbhagat1 week ago
SIMPLE JAVA SOLUTION
class Solution{
    
    int solve(int n, int x, int y, int z, int dp[]){
        if(n==0) return 0;
        if(n<0) return -(int)1e8;
        if(dp[n]!=-1) return dp[n];
        int max = Math.max(solve(n-x,x,y,z,dp),Math.max(solve(n-y,x,y,z,dp),solve(n-z,x,y,z,dp)));
        return dp[n] = max==-1e8?-(int)1e8:1+max;
    }
    
    //Function to find the maximum number of cuts.
    public int maximizeCuts(int n, int x, int y, int z){
       //Your code here
       int dp[] = new int[n+1];
       Arrays.fill(dp,-1);
       int ans = solve(n,x,y,z,dp);
       return ans==-1e8?0:ans;
    }
}
+1
scien-terrific2 weeks ago
class Solution
{
    public:
    //Function to find the maximum number of cuts.
    int maximizeTheCuts(int n, int x, int y, int z)
    {
        vector<int>dp(n+1,-1);
        dp[0]=0;
        for(int i=1;i<=n;i++){
            if(i>=x)dp[i]=max(dp[i],dp[i-x]);
            if(i>=y)dp[i]=max(dp[i],dp[i-y]);
            if(i>=z)dp[i]=max(dp[i],dp[i-z]);
            if(dp[i]!=-1)dp[i]++;
        }
        return max(dp[n],0);
    }
};
0
aniketsaraswat1122 weeks ago
int maximizeTheCuts(int n, int x, int y, int z){
        int dp[n+1] = {};
        if(x <= n) dp[x] = 1;
        if(y <= n) dp[y] = 1;
        if(z <= n) dp[z] = 1;
        if(x == 1 || y == 1 || z == 1) return n;
        int element = min(x,min(y,z));
        for(int i = element; i <= n; i++){
            int dx = 0, dy = 0, dz = 0;
            if(x <= i) dx = dp[i-x];
            if(y <= i) dy = dp[i-y];
            if(z <= i) dz = dp[i-z];
            int element = max(dx,max(dy,dz));
            if(element) dp[i] = max(dp[i],1 + element);
        }
        return dp[n];
    }
+1
ankitcasingh3 weeks ago
//Function to find the maximum number of cuts.
    int dp[10001];
    int maximizeTheCut(int n, int x, int y, int z)
    {
        if(n==0){
            return 0;
        }
        if(n<0){
        return INT_MIN;    
        }
        if(dp[n]!=-1){
            return dp[n];
        }
        return dp[n]=max(1+maximizeTheCut(n-x,x,y,z),max(1+maximizeTheCut(n-y,x,y,z),
        1+maximizeTheCut(n-z,x,y,z)));
    }
    int maximizeTheCuts(int n, int x, int y, int z)
    {   for(int i=0;i<n+1;i++){
            dp[i]=-1;
        }
        if(maximizeTheCut(n,x,y,z)<0){
            return 0;
        }
        return maximizeTheCut(n,x,y,z);
        
    }
-1
jainmuskan5653 weeks ago
int dp[10004];   int helper(int n,int x,int y,int z){       if(n==0){           return 0;       }       if(dp[n]!= -1){           return dp[n];       }       int opt1= INT_MIN, opt2= INT_MIN, opt3= INT_MIN;       if(x<=n){           opt1= helper(n-x,x,y,z);       }       if(y<=n){           opt2= helper(n-y,x,y,z);       }       if(z<=n){           opt3= helper(n-z,x,y,z);       }       return dp[n]=1+ max(opt1,max(opt2,opt3));   }   int maximizeTheCuts(int n, int x, int y, int z)   {       memset(dp,-1,sizeof(dp));       int a= helper(n,x,y,z);       if(a<0){           return 0;       }       return a;   }
0
adityagagtiwari1 month ago
we can use greedy and it will be super easy to solve this problem.
Just find the smallest cut and use just that to make cuts
+1
19ucs2101 month ago
Easy simple understandable solution
int maximizeTheCuts(int n, int x, int y, int z)
   {
       //Your code here
       int dp[n+1];
       for(int i=0;i<=n;i++)
           dp[i]=-1;
       dp[0]=0;
       for(int i=1;i<=n;i++)
       {
           int t1=-1,t2=-1,t3=-1;
           if(x<=i)
               t1=dp[i-x];
           if(y<=i)
               t2=dp[i-y];
           if(z<=i)
               t3=dp[i-z];
           if(t1>=0||t2>=0||t3>=0)
               dp[i]=1+max({t1,t2,t3});
       }
       return max(dp[n],0);
   }
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.