Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 30 commits into from
Sep 30, 2020
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b15bd1e
Create largestBST_in_binary_tree.cpp
anishmo99 Sep 21, 2020
9825a3a
formatting filenames b15bd1ea
Sep 21, 2020
44737bd
updating DIRECTORY.md
Sep 21, 2020
6319f2e
Update DIRECTORY.md
anishmo99 Sep 21, 2020
46ac5a4
updating DIRECTORY.md
Sep 21, 2020
4d211fc
fixed compilation error in cut_rod.cpp code
Pardeep009 Sep 23, 2020
698396b
fixed clang-tidy warnings
Pardeep009 Sep 24, 2020
0775057
Merge branch 'master' into pardeep/bugFix
Pardeep009 Sep 24, 2020
110ae88
Delete largestbst_in_binary_tree.cpp
Pardeep009 Sep 24, 2020
c104ead
removed compilation errors
Pardeep009 Sep 24, 2020
9ce09fa
Update cut_rod.cpp
Pardeep009 Sep 24, 2020
777bc03
added requested changes in the code
Pardeep009 Sep 25, 2020
256fc49
added testing
Pardeep009 Sep 25, 2020
ccb35a7
Merge branch 'pardeep/bugFix' of https://github.com/Pardeep009/C-Plus…
Pardeep009 Sep 25, 2020
f9593fc
Update DIRECTORY.md
Pardeep009 Sep 25, 2020
2bfb914
Delete largestbst_in_binary_tree.cpp
Pardeep009 Sep 25, 2020
8c866d7
added namespaces
Pardeep009 Sep 25, 2020
8b6bd5a
Merge branch 'pardeep/bugFix' of https://github.com/Pardeep009/C-Plus…
Pardeep009 Sep 25, 2020
19e592d
added kadane2 algorithm
Pardeep009 Sep 25, 2020
5f324ce
Update dynamic_programming/kadane2.cpp
Pardeep009 Sep 25, 2020
b803a37
Update dynamic_programming/kadane2.cpp
Pardeep009 Sep 25, 2020
37a836a
Update dynamic_programming/cut_rod.cpp
Pardeep009 Sep 25, 2020
14a4f46
Update dynamic_programming/cut_rod.cpp
Pardeep009 Sep 25, 2020
058b2ab
Update dynamic_programming/cut_rod.cpp
Pardeep009 Sep 25, 2020
631691d
added right funtion name in comments
Pardeep009 Sep 25, 2020
02cdd50
Update dynamic_programming/cut_rod.cpp
Pardeep009 Sep 25, 2020
9681e86
Update dynamic_programming/cut_rod.cpp
Pardeep009 Sep 25, 2020
29bd27c
added documentation for template parameter
Pardeep009 Sep 25, 2020
db70ae2
checking for github actions
Pardeep009 Sep 25, 2020
700496e
clang-format and clang-tidy fixes for db70ae2f
Sep 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added testing
  • Loading branch information
Pardeep009 committed Sep 25, 2020
commit 256fc499a4b08dc27fa3bb817bf76bb12ba1d9bc
41 changes: 32 additions & 9 deletions dynamic_programming/cut_rod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
*
* @details
* Given a rod of length n inches and an array of prices that
* contains prices of all pieces of size smaller than n. Determine
* contains prices of all pieces of size<=n. Determine
* the maximum profit obtainable by cutting up the rod and selling
* the pieces.
*
* @author [Anmol](https://github.com/Anmol3299)
* @author [Pardeep](https://github.com/Pardeep009)
*
*/

#include <array>
#include <cassert>
#include <climits>
#include <iostream>

Expand Down Expand Up @@ -46,18 +50,37 @@ int cut_rod(const std::array<int, T> &price, const int n) {
}

/**
* @brief Main function
* @returns 0 on exit
* Function to test above algorithm
Pardeep009 marked this conversation as resolved.
Show resolved Hide resolved
*/
int main() {
const int n = 30; // size of rod
std::array<int, n> price = {
void test() {
Pardeep009 marked this conversation as resolved.
Show resolved Hide resolved
// 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 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 = cut_rod(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;
}

// maximum profit
std::cout << cut_rod(price, n);

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
Pardeep009 marked this conversation as resolved.
Show resolved Hide resolved
// Testing
test();
return 0;
}