From c19b4896c17ac8eaa3f946bde4a55598b123fd2f Mon Sep 17 00:00:00 2001 From: ABHISHEK-821005 <69668943+ABHISHEK-821005@users.noreply.github.com> Date: Sun, 25 Oct 2020 20:55:58 +0530 Subject: [PATCH 1/6] improved time complexity if all the price are same then we can do it in o(n). improved time complexity. --- dynamic_programming/cut_rod.cpp | 56 ++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) 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 = { From e272befbd7f2ea4a8d899e816f9cba0e2c92ef07 Mon Sep 17 00:00:00 2001 From: ABHISHEK-821005 <69668943+ABHISHEK-821005@users.noreply.github.com> Date: Mon, 26 Oct 2020 12:51:34 +0530 Subject: [PATCH 2/6] updated time complexity if all the prices are same case(o(n)). --- dynamic_programming/cut_rod.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/dynamic_programming/cut_rod.cpp b/dynamic_programming/cut_rod.cpp index e67c3eb4a..5aeb801f5 100644 --- a/dynamic_programming/cut_rod.cpp +++ b/dynamic_programming/cut_rod.cpp @@ -61,18 +61,11 @@ int maxProfitByCuttingRod(const std::array &price, const int n) { delete[] profit; return ans; // returning maximum profit } -} // namespace cut_rod -} // namespace dynamic_programming - -/** - * @brief Function to test above algorithm - * @returns void - */ template -bool WhatIfAllPricesAreSame(const std::array &price, const int n){ +bool WhatIfAllPricesAreSame(const std::array &price, const uint64_t &n){ /* @@ -108,7 +101,18 @@ CR(0) } return true; -} +}// checks whether all the prices are same or not + + +} // namespace cut_rod +} // namespace dynamic_programming + +/** + * @brief Function to test above algorithm + * @returns void + */ + + static void test() { @@ -119,7 +123,7 @@ static void test() { dynamic_programming::cut_rod::maxProfitByCuttingRod(price1, n1); const int expected_max_profit1 = 22; - if( WhatIfAllPricesAreSame(price1,n1)){ + if(dynamic_programming::cut_rod::WhatIfAllPricesAreSame(price1,n1)){ std::cout << "Maximum profit with " << n1 << " inch road is " <<(n1)*price1[0] << std::endl; } From b76e41bf2ee62eba85995fe6711cff93aa9c9087 Mon Sep 17 00:00:00 2001 From: ABHISHEK-821005 <69668943+ABHISHEK-821005@users.noreply.github.com> Date: Wed, 28 Oct 2020 17:53:40 +0530 Subject: [PATCH 3/6] added a new function to improve time complexity WhatIfAllPricesAreSame(price1,n1) added this function. --- dynamic_programming/cut_rod.cpp | 49 ++++++--------------------------- 1 file changed, 9 insertions(+), 40 deletions(-) diff --git a/dynamic_programming/cut_rod.cpp b/dynamic_programming/cut_rod.cpp index 5aeb801f5..cbfd68298 100644 --- a/dynamic_programming/cut_rod.cpp +++ b/dynamic_programming/cut_rod.cpp @@ -41,7 +41,7 @@ namespace cut_rod { * @return maximum profit obtainable for @param n inch rod. */ template -int maxProfitByCuttingRod(const std::array &price, const int n) { +int maxProfitByCuttingRod(const std::array &price, const uint64_t &n) { int *profit = new int[n + 1]; // profit[i] will hold maximum profit for i inch rod @@ -62,48 +62,23 @@ int maxProfitByCuttingRod(const std::array &price, const int n) { return ans; // returning maximum profit } - - template bool WhatIfAllPricesAreSame(const std::array &price, const uint64_t &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) - - + Reason::--> 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]; + const int16_t temp=price[0]; for (size_t i = 1; i price1 = {1, 1,1,1,1,1,1,1}; // price array - const int max_profit1 = + const int16_t n1 = 8; // size of rod + std::array price1 = {1,1,1,1,1,1,1,1}; // price array + const int64_t max_profit1 = dynamic_programming::cut_rod::maxProfitByCuttingRod(price1, n1); - const int expected_max_profit1 = 22; + const int32_t expected_max_profit1 = 22; - if(dynamic_programming::cut_rod::WhatIfAllPricesAreSame(price1,n1)){ + if (dynamic_programming::cut_rod::WhatIfAllPricesAreSame(price1,n1)) { std::cout << "Maximum profit with " << n1 << " inch road is " <<(n1)*price1[0] << std::endl; } - else{ + 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 = { From 063e36922be087abab3dacd0320802d489f72b0c Mon Sep 17 00:00:00 2001 From: ABHISHEK-821005 <69668943+ABHISHEK-821005@users.noreply.github.com> Date: Fri, 30 Oct 2020 19:06:04 +0530 Subject: [PATCH 4/6] updated references --- dynamic_programming/cut_rod.cpp | 48 ++++++++------------------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/dynamic_programming/cut_rod.cpp b/dynamic_programming/cut_rod.cpp index cbfd68298..c45c8b2be 100644 --- a/dynamic_programming/cut_rod.cpp +++ b/dynamic_programming/cut_rod.cpp @@ -57,28 +57,10 @@ int maxProfitByCuttingRod(const std::array &price, const uint64_t &n) { } profit[i] = q; } - int ans = profit[n]; + const int16_t ans = profit[n]; delete[] profit; return ans; // returning maximum profit } - -template -bool WhatIfAllPricesAreSame(const std::array &price, const uint64_t &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::--> - 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 int16_t temp=price[0]; - for (size_t i = 1; i &price, const uint64_t &n){ * @brief Function to test above algorithm * @returns void */ - static void test() { // Test 1 const int16_t n1 = 8; // size of rod - std::array price1 = {1,1,1,1,1,1,1,1}; // price array - const int64_t max_profit1 = + std::array price1 = {1,2,4,6,8,45,21,9}; // price array + const int64_t max_profit1 = dynamic_programming::cut_rod::maxProfitByCuttingRod(price1, n1); - const int32_t expected_max_profit1 = 22; - - if (dynamic_programming::cut_rod::WhatIfAllPricesAreSame(price1,n1)) { - std::cout << "Maximum profit with " << n1 << " inch road is " <<(n1)*price1[0] - << std::endl; - } - else { - - assert(max_profit1 == expected_max_profit1); + const int64_t expected_max_profit1 = 47; + 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 = { + const int16_t n2 = 30; // size of rod + std::array price2 = { 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}; - const int max_profit2 = + + const int64_t max_profit2= dynamic_programming::cut_rod::maxProfitByCuttingRod(price2, n2); - const int expected_max_profit2 = 90; - assert(max_profit2 == expected_max_profit2); + const int32_t expected_max_profit2 = 90; + assert(max_profit2 == expected_max_profit2); std::cout << "Maximum profit with " << n2 << " inch road is " << max_profit2 << std::endl; } From 6556e0da3ce0b92b90e0e9b0c1531238eb7fec05 Mon Sep 17 00:00:00 2001 From: ABHISHEK-821005 <69668943+ABHISHEK-821005@users.noreply.github.com> Date: Sat, 31 Oct 2020 11:46:30 +0530 Subject: [PATCH 5/6] Update cut_rod.cpp --- dynamic_programming/cut_rod.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/cut_rod.cpp b/dynamic_programming/cut_rod.cpp index c45c8b2be..80e4391d2 100644 --- a/dynamic_programming/cut_rod.cpp +++ b/dynamic_programming/cut_rod.cpp @@ -71,11 +71,11 @@ int maxProfitByCuttingRod(const std::array &price, const uint64_t &n) { static void test() { // Test 1 const int16_t n1 = 8; // size of rod - std::array price1 = {1,2,4,6,8,45,21,9}; // price array - const int64_t max_profit1 = + std::array price1 = {1,2,4,6,8,45,21,9}; // price array + const int64_t max_profit1 = dynamic_programming::cut_rod::maxProfitByCuttingRod(price1, n1); - const int64_t expected_max_profit1 = 47; - assert(max_profit1 == expected_max_profit1); + const int64_t expected_max_profit1 = 47; + assert(max_profit1 == expected_max_profit1); std::cout << "Maximum profit with " << n1 << " inch road is " << max_profit1 << std::endl; @@ -86,10 +86,10 @@ static void test() { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; - const int64_t max_profit2= + const int64_t max_profit2= dynamic_programming::cut_rod::maxProfitByCuttingRod(price2, n2); - const int32_t expected_max_profit2 = 90; - assert(max_profit2 == expected_max_profit2); + const int32_t expected_max_profit2 = 90; + assert(max_profit2 == expected_max_profit2); std::cout << "Maximum profit with " << n2 << " inch road is " << max_profit2 << std::endl; } From 2cc9100f83738d9011d1301218adbadbfc2038e3 Mon Sep 17 00:00:00 2001 From: ABHISHEK-821005 <69668943+ABHISHEK-821005@users.noreply.github.com> Date: Sat, 31 Oct 2020 11:53:40 +0530 Subject: [PATCH 6/6] added a new example to check correctness of the code --- dynamic_programming/cut_rod.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dynamic_programming/cut_rod.cpp b/dynamic_programming/cut_rod.cpp index 80e4391d2..c365be4fc 100644 --- a/dynamic_programming/cut_rod.cpp +++ b/dynamic_programming/cut_rod.cpp @@ -92,6 +92,15 @@ static void test() { assert(max_profit2 == expected_max_profit2); std::cout << "Maximum profit with " << n2 << " inch road is " << max_profit2 << std::endl; + // Test 3 + const int16_t n3 = 5; // size of rod + std::array price3 = {2,9,17,23,45}; // price array + const int64_t max_profit3 = + dynamic_programming::cut_rod::maxProfitByCuttingRod(price3, n3); + const int64_t expected_max_profit3 = 45; + assert(max_profit3 == expected_max_profit3); + std::cout << "Maximum profit with " << n3 << " inch road is " << max_profit3 + << std::endl; } /**