[fix]: dynamic_programming/cut_rod.cpp does not compile (#1085)

* Create largestBST_in_binary_tree.cpp

* formatting filenames b15bd1ea

* updating DIRECTORY.md

* Update DIRECTORY.md

* updating DIRECTORY.md

* fixed compilation error in cut_rod.cpp code

* fixed clang-tidy warnings

* Delete largestbst_in_binary_tree.cpp

* removed compilation errors

* Update cut_rod.cpp

* added requested changes in the code

* added testing

* Update DIRECTORY.md

* Delete largestbst_in_binary_tree.cpp

* added namespaces

* added kadane2 algorithm

* Update dynamic_programming/kadane2.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update dynamic_programming/kadane2.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update dynamic_programming/cut_rod.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update dynamic_programming/cut_rod.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update dynamic_programming/cut_rod.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* added right funtion name in comments

* Update dynamic_programming/cut_rod.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update dynamic_programming/cut_rod.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* added documentation for template parameter

* checking for github actions

* clang-format and clang-tidy fixes for db70ae2f

Co-authored-by: anishmo99 <ani10sh@gmail.com>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Anish Mookherjee <59157112+anishmo99@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Pardeep Bhatt 2020-09-30 23:33:34 +05:30 committed by GitHub
parent 8e14b25fa3
commit b09b3da69a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 17 deletions

View File

@ -1,25 +1,104 @@
/*Given a rod of length n inches and an array of prices that /**
contains prices of all pieces of size smaller than n. Determine * @file
the maximum value obtainable by cutting up the rod and selling * @brief Implementation of cutting a rod problem
the pieces.*/ *
* @details
* Given a rod of length n inches and an array of prices that
* contains prices of all pieces of size<=n. Determine
* the maximum profit obtainable by cutting up the rod and selling
* the pieces.
*
* ### Algorithm
* The idea is to break the given rod into every smaller piece as possible
* and then check profit for each piece, by calculating maximum profit for
* smaller pieces we will build the solution for larger pieces in bottom-up
* manner.
*
* @author [Anmol](https://github.com/Anmol3299)
* @author [Pardeep](https://github.com/Pardeep009)
*/
#include <array>
#include <cassert>
#include <climits>
#include <iostream> #include <iostream>
using namespace std; /**
int cutrod(int p[], int n) { * @namespace dynamic_programming
int r[n + 1]; * @brief Dynamic Programming algorithms
r[0] = 0; */
for (int j = 0; j < n; j++) { namespace dynamic_programming {
/**
* @namespace cut_rod
* @brief Implementation of cutting a rod problem
*/
namespace cut_rod {
/**
* @brief Cuts the rod in different pieces and
* stores the maximum profit for each piece of the rod.
* @tparam T size of the price array
* @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.
*/
template <size_t T>
int maxProfitByCuttingRod(const std::array<int, T> &price, const int n) {
int *profit =
new int[n + 1]; // profit[i] will hold maximum profit for i inch rod
profit[0] = 0; // if length of rod is zero, then no profit
// 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; int q = INT_MIN;
for (int i = 0; i <= j; i++) { for (size_t j = 1; j <= i; j++) {
q = max(q, p[i] + r[j - i]); q = std::max(q, price[j - 1] + profit[i - j]);
} }
r[j + 1] = q; profit[i] = q;
} }
return r[n]; int ans = profit[n];
delete[] profit;
return ans; // returning maximum profit
} }
} // namespace cut_rod
} // namespace dynamic_programming
/**
* @brief Function to test above algorithm
* @returns void
*/
static void test() {
// Test 1
const int n1 = 8; // size of rod
std::array<int, n1> price1 = {1, 5, 8, 9, 10, 17, 17, 20}; // price array
const int max_profit1 =
dynamic_programming::cut_rod::maxProfitByCuttingRod(price1, n1);
const int expected_max_profit1 = 22;
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<int, n2> 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 =
dynamic_programming::cut_rod::maxProfitByCuttingRod(price2, n2);
const int expected_max_profit2 = 90;
assert(max_profit2 == expected_max_profit2);
std::cout << "Maximum profit with " << n2 << " inch road is " << max_profit2
<< std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() { int main() {
int price[] = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30, 31, 32, 33, 34, 35, // Testing
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; test();
cout << cutrod(price, 30);
return 0; return 0;
} }

View File

@ -27,7 +27,8 @@
namespace dynamic_programming { namespace dynamic_programming {
/** /**
* @namespace kadane * @namespace kadane
* @brief Functions for [Kadane](https://en.wikipedia.org/wiki/Kadane%27s_algorithm) algorithm. * @brief Functions for
* [Kadane](https://en.wikipedia.org/wiki/Kadane%27s_algorithm) algorithm.
*/ */
namespace kadane { namespace kadane {
/** /**