diff --git a/dynamic_programming/cut_rod.cpp b/dynamic_programming/cut_rod.cpp index 9513e487e..e67c3eb4a 100644 --- a/dynamic_programming/cut_rod.cpp +++ b/dynamic_programming/cut_rod.cpp @@ -68,17 +68,71 @@ int maxProfitByCuttingRod(const std::array &price, const int n) { * @brief Function to test above algorithm * @returns void */ + + + +template +bool WhatIfAllPricesAreSame(const std::array &price, const int n){ + + /* + + Note that if all the prices of the different lengths of rod are same the answer will be always n*price; + where price=price of 1 length rod + Reason:: + cR() ---> cutRod() + + cR(4) + / / + / / + cR(3) cR(2) cR(1) cR(0) + / | / | + / | / | + cR(2) cR(1) cR(0) cR(1) cR(0) cR(0) + / | | + / | | + cR(1) cR(0) cR(0) cR(0) + / + / +CR(0) + + + if every length has same price , you would definitely want the rod of length 1, with n quantities. + which will give us maximum profits and maximum cuts. + + */ + + const int temp=price[0]; + for (size_t i = 1; i price1 = {1, 5, 8, 9, 10, 17, 17, 20}; // price array + std::array price1 = {1, 1,1,1,1,1,1,1}; // price array const int max_profit1 = dynamic_programming::cut_rod::maxProfitByCuttingRod(price1, n1); const int expected_max_profit1 = 22; + + if( WhatIfAllPricesAreSame(price1,n1)){ + std::cout << "Maximum profit with " << n1 << " inch road is " <<(n1)*price1[0] + << std::endl; + } + else{ + assert(max_profit1 == expected_max_profit1); std::cout << "Maximum profit with " << n1 << " inch road is " << max_profit1 << std::endl; + } + + + // Test 2 const int n2 = 30; // size of rod std::array price2 = {