Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
601 B
2
Indexable
#define INT_MIN -10000000
#include<vector>
int cuts(int n,int &x,int &y,int &z,vector<int>&dp){//dp top down
	if(n==0) return 0;
	if(n<0) return INT_MIN;
	if(dp[n]!=-1) return dp[n];
	int a=cuts(n-x,x,y,z,dp);
	int b=cuts(n-y,x,y,z,dp);
	int c=cuts(n-z,x,y,z,dp);
	int ans=max(a,max(b,c)) +1;
	dp[n]=ans;
	return ans;
}

int cutSegments(int n, int x, int y, int z) {
	//find max no of cuts that can be made in a rod
	vector<int> dp(n+1,-1);
	int ans= cuts(n,x,y,z,dp);
	if(ans<0) return 0;
	return ans;
	//tabulation
	dp[0]=0;
	for(int i=1;i<=n;i++){
		dp[i]=max(dp[i])
	}
}