mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-09 15:43:48 +08:00
added namespaces
This commit is contained in:
@@ -8,6 +8,12 @@
|
||||
* 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)
|
||||
*
|
||||
@@ -17,22 +23,29 @@
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
|
||||
template <size_t T>
|
||||
|
||||
/**
|
||||
* This function cuts the rod in different pieces and stores the maximum profit
|
||||
* for each piece of the rod.
|
||||
* @namespace dynamic_programming
|
||||
* @brief Dynamic Programming algorithms
|
||||
*/
|
||||
namespace dynamic_programming {
|
||||
/**
|
||||
* @namespace cut_rod
|
||||
* @brief Implementation of cutting a rod problem
|
||||
*/
|
||||
namespace cut_rod {
|
||||
/**
|
||||
* @brief cut_rod 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<int, T> &price, const int n) {
|
||||
// profit[i] will hold maximum profit for i inch rod
|
||||
int *profit = new int[n + 1];
|
||||
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
|
||||
|
||||
// if length of rod is zero, then no profit
|
||||
profit[0] = 0;
|
||||
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
|
||||
@@ -46,8 +59,10 @@ int cut_rod(const std::array<int, T> &price, const int n) {
|
||||
}
|
||||
int ans = profit[n];
|
||||
delete[] profit;
|
||||
return ans; /** return maximum profit obtainable for @param n inch rod */
|
||||
return ans; // returning maximum profit
|
||||
}
|
||||
} // namespace cut_rod
|
||||
} // namespace dynamic_programming
|
||||
|
||||
/**
|
||||
* Function to test above algorithm
|
||||
@@ -56,7 +71,8 @@ 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 = cut_rod(price1, n1);
|
||||
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
|
||||
@@ -68,7 +84,8 @@ void test() {
|
||||
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 = cut_rod(price2, n2);
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user