|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation of cutting a rod problem. More...
#include <array>#include <cassert>#include <climits>#include <iostream>Namespaces | |
| namespace | dynamic_programming |
| Dynamic Programming algorithms. | |
| namespace | cut_rod |
| Implementation of cutting a rod problem. | |
Functions | |
| template<size_t T> | |
| int | dynamic_programming::cut_rod::maxProfitByCuttingRod (const std::array< int, T > &price, const uint64_t &n) |
| Cuts the rod in different pieces and stores the maximum profit for each piece of the rod. More... | |
| static void | test () |
| Function to test above algorithm. More... | |
| int | main () |
| Main function. More... | |
Implementation of cutting a rod problem.
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.
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.
| int main | ( | void | ) |
| int dynamic_programming::cut_rod::maxProfitByCuttingRod | ( | const std::array< int, T > & | price, |
| const uint64_t & | n | ||
| ) |
Cuts the rod in different pieces and stores the maximum profit for each piece of the rod.
| T | size of the price array |
| n | size of the rod in inches |
| price | an array of prices that contains prices of all pieces of size<=n |
| n | inch rod. |
|
static |
Function to test above algorithm.