-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_MaximizeTheCutSegments.cpp
79 lines (61 loc) · 1.52 KB
/
GFG_MaximizeTheCutSegments.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
https://practice.geeksforgeeks.org/problems/cutted-segments1642/1/#
Maximize The Cut Segments
*/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
vector<int> cutparts;
//Function to find the maximum number of cuts.
int maximizeTheCuts(int n, int x, int y, int z)
{
cutparts.push_back(x);
cutparts.push_back(y);
cutparts.push_back(z);
vector<int> memo(n+1, -1);
solve(n, memo) ;
return memo[n] == INT_MIN ? 0 : memo[n];
}
int solve(int len, vector<int>& memo)
{
if(len == 0)
return memo[len]=0;
if(memo[len] != -1)
return memo[len];
int counts=0;
for(int cut: cutparts)
{
if(len>=cut)
{
// cout<<len<<" "<<cut<<"| ";
counts = max(counts, solve(len-cut, memo)+1);
}
}
if(counts == 0)
return memo[len] = INT_MIN;
return memo[len] = counts;
}
};
// { Driver Code Starts.
int main() {
//taking testcases
int t;
cin >> t;
while(t--)
{
//taking length of line segment
int n;
cin >> n;
//taking types of segments
int x,y,z;
cin>>x>>y>>z;
Solution obj;
//calling function maximizeTheCuts()
cout<<obj.maximizeTheCuts(n,x,y,z)<<endl;
}
return 0;
} // } Driver Code Ends