From 9aae0fe5ea02f1bc1b2a4766307549145195a885 Mon Sep 17 00:00:00 2001 From: Jyoti Singh <98025162+dev24il@users.noreply.github.com> Date: Sat, 15 Oct 2022 21:22:10 +0530 Subject: [PATCH] chore(CPlusPlus): add rod cutting problem (#985) * Rod cutting in cpp completed * Update rod-cutting.cpp --- .../Dynamic-Programming/rod-cutting.cpp | 76 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 77 insertions(+) create mode 100644 algorithms/CPlusPlus/Dynamic-Programming/rod-cutting.cpp diff --git a/algorithms/CPlusPlus/Dynamic-Programming/rod-cutting.cpp b/algorithms/CPlusPlus/Dynamic-Programming/rod-cutting.cpp new file mode 100644 index 00000000..40e2b876 --- /dev/null +++ b/algorithms/CPlusPlus/Dynamic-Programming/rod-cutting.cpp @@ -0,0 +1,76 @@ +// Rod Cutting Problem +// Given a rod of length n and a list of rod prices of length i, where 1 <= i <= n, find the optimal way to cut the rod into smaller rods to maximize profit. + +// Rod Cutting Optimal Approach +// We will solve this problem in a bottom-up manner. (iteratively) +// In the bottom-up approach, we solve smaller subproblems first, then move on to larger subproblems. +// The following bottom-up approach computes dp[i], which stores maximum profit achieved from the rod of length i from 1 to len. +// It uses the value of smaller values i already computed. + +// Space complexity: O(n) +// Time complexity: O(n^n) + + +// Solution +#include +#include +#include + +using namespace std; + +// Function to find the maximum revenue from cutting a rod of length (len) +// where the rod of length (i) has cost (prices[i - 1]) +int RodCutting(vector &prices, int len) +{ + // (dp) stores the maximum revenue achieved from cutting a rod of length (from 1 to len) + vector dp(len + 1, 0); + // If the rod length is negative (invalid) or zero there's no revenue from it + if (len <= 0) + { + return 0; + } + // Cut a rod of length (i) + for (int i = 1; i <= len; i++) + { + // divide the rod of length (i) into two rods of lengths (j) and (i - j) + // and store the maximum revenue + for (int j = 0; j < i; j++) + { + // (dp[i]) stores the maximum revenue achieved from cutting a rod of length (i) + dp[i] = max(dp[i], prices[j] + dp[i - j - 1]); + } + } + // (dp[len]) contains the maximum revenue from cutting a rod of length (len) + return dp[len]; +} + +int main() +{ + int len; + cout << "Enter the rod length :"; + cin >> len; + + vector prices(len); + for (int i = 1; i <= len; i++) + { + cout << "Enter the price of the rod of length " << i << " :"; + cin >> prices[i - 1]; + } + + cout << "Maximum revenue = " << RodCutting(prices, len); + return 0; +} + + +// Input and output: +// 1. prices[] = [1, 5, 8, 9, 10, 17, 17, 20] +// rod length = 4 +// Best: Cut the rod into two pieces of length 2 each to gain revenue of 5 + 5 = 10 + +// 2. prices[] = [1, 5, 8, 9, 10, 17, 17, 20] +// rod length = 8 +// Best: Cut the rod into two pieces of length 2 and 6 to gain revenue of 5 + 17 = 22 + +// 3. prices[] = [3, 5, 8, 9, 10, 17, 17, 20] +// rod length = 8 +// Best: Cut the rod into eight pieces of length 1 to gain revenue of 8 * 3 = 24 diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 44469338..a5cca706 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -42,6 +42,7 @@ - [Matrix chain Multiplication](Dynamic-Programming/matrix-chain-multiplication.cpp) - [Edit Distance](Dynamic-Programming/edit-distance.cpp) - [Fibonacci](Dynamic-Programming/fibonacci.cpp) +- [Rod Cutting](Dynamic-Programming/rod-cutting.cpp) ## Graphs