Update cut_rod.cpp

This commit is contained in:
Pardeep Bhatt
2020-09-24 14:13:34 +05:30
committed by GitHub
parent 110ae88aaf
commit 9ce09fa373

View File

@@ -11,8 +11,8 @@ using std::cout;
template <size_t T>
int cutrod(const std::array<int, T> &p, int n) {
std::array<int, n + 1> r;
int cutrod(const std::array<int, T> &p, const int n) {
int *r = new int[n + 1];
r[0] = 0;
for (int j = 0; j < n; j++) {
int q = INT_MIN;
@@ -21,12 +21,15 @@ int cutrod(const std::array<int, T> &p, int n) {
}
r[j + 1] = q;
}
return r[n];
int ans = r[n];
delete[] r;
return ans;
}
int main() {
std::array<int, 30> price = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
cout << cutrod(price, 30);
const int n = 30;
std::array<int, n> price = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
cout << cutrod(price, n);
return 0;
}