forked from aishwarydewangan/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmax_ribbon_cut.cpp
64 lines (49 loc) · 1.39 KB
/
max_ribbon_cut.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
/*
We are given a ribbon of length ‘n’ and a set of possible ribbon lengths. Now we need to cut the
ribbon into the maximum number of pieces that comply with the above-mentioned possible lengths. Write a
method that will return the count of pieces.
Example 1:
n: 5
Ribbon Lengths: {2,3,5}
Output: 2
Explanation: Ribbon pieces will be {2,3}.
Example 2:
n: 7
Ribbon Lengths: {2,3}
Output: 3
Explanation: Ribbon pieces will be {2,2,3}.
Example 3:
n: 13
Ribbon Lengths: {3,5,7}
Output: 3
Explanation: Ribbon pieces will be {3,3,7}.
*/
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> memo;
int ribbon(vector<int>& coins, int amount, int index) {
if(index == coins.size()) {
if(amount == 0)
return 0;
return INT_MIN;
}
if(memo[index][amount] == -1) {
int c1 = INT_MIN;
if(coins[index] <= amount) {
c1 = ribbon(coins, amount-coins[index], index);
if(c1 != INT_MIN) {
c1 += 1;
}
}
int c2 = ribbon(coins, amount, index+1);
memo[index][amount] = max(c1, c2);
}
return memo[index][amount];
}
int main() {
vector<int> coins = {2, 3, 5};
int amount = 5;
memo.resize(coins.size(), vector<int>(amount+1, -1));
cout << "Maximum Ribbon Cut: " << ribbon(coins, amount, 0);
return 0;
}