TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of cutting a rod problem. More...
#include <array>
#include <cassert>
#include <climits>
#include <cstdint>
#include <iostream>
Go to the source code of this file.
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. | |
static void | test () |
Function to test above algorithm. | |
int | main () |
Main function. | |
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.
Definition in file cut_rod.cpp.
int main | ( | void | ) |
Main function.
Definition at line 111 of file cut_rod.cpp.
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. |
Definition at line 45 of file cut_rod.cpp.
|
static |
Function to test above algorithm.
Definition at line 72 of file cut_rod.cpp.