From 777bc0374dc9825933d440fb3416f17503e20655 Mon Sep 17 00:00:00 2001 From: Pardeep Bhatt Date: Fri, 25 Sep 2020 16:14:02 +0530 Subject: [PATCH] added requested changes in the code --- dynamic_programming/cut_rod.cpp | 70 +++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/dynamic_programming/cut_rod.cpp b/dynamic_programming/cut_rod.cpp index 0d566cc8c..38b02926a 100644 --- a/dynamic_programming/cut_rod.cpp +++ b/dynamic_programming/cut_rod.cpp @@ -1,35 +1,63 @@ -/*Given a rod of length n inches and an array of prices that -contains prices of all pieces of size smaller than n. Determine -the maximum value obtainable by cutting up the rod and selling -the pieces.*/ +/** + * @file cut_rod.cpp + * @brief Implementation of cutting a rod problem + * + * @details + * Given a rod of length n inches and an array of prices that + * contains prices of all pieces of size smaller than n. Determine + * the maximum profit obtainable by cutting up the rod and selling + * the pieces. + * + */ #include #include #include -using std::cout; - template -int cutrod(const std::array &p, const int n) { - int *r = new int[n + 1]; - r[0] = 0; - for (int j = 0; j < n; j++) { +/** + * This function cuts the rod in different pieces and stores the maximum profit + * for each piece of the rod. + * @param n size of the rod in inches + * @param price an array of prices that contains prices of all pieces of size<=n + * @return maximum profit obtainable for @param n inch rod. + */ +int cut_rod(const std::array &price, const int n) { + // profit[i] will hold maximum profit for i inch rod + int *profit = new int[n + 1]; + + // if length of rod is zero, then no profit + profit[0] = 0; + + // outer loop will select size of rod, starting from 1 inch to n inch rod. + // inner loop will evaluate the maximum profit we can get for i inch rod by + // making every possible cut on it and will store it in profit[i]. + for (size_t i = 1; i <= n; i++) { int q = INT_MIN; - for (int i = 0; i <= j; i++) { - q = std::max(q, p[i] + r[j - i]); + for (size_t j = 1; j <= i; j++) { + q = std::max(q, price[j - 1] + profit[i - j]); } - r[j + 1] = q; + profit[i] = q; } - int ans = r[n]; - delete[] r; - return ans; + int ans = profit[n]; + delete[] profit; + return ans; /** return maximum profit obtainable for @param n inch rod */ } + +/** + * @brief Main function + * @returns 0 on exit + */ int main() { - const int n = 30; - std::array 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); + const int n = 30; // size of rod + std::array price = { + 1, 5, 8, 9, 10, 17, 17, 20, 24, 30, // price array + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; + + // maximum profit + std::cout << cut_rod(price, n); + return 0; }