Skip to content

Feature dp implementation #81

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

Merged
merged 7 commits into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions include/0005_DynamicProgramming/0001_RodCutting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include<iostream>
#include<vector>
using namespace std;

namespace RodCutting
{
class DynamicProgramming
{
private:
int _totalLength;
vector<int> _price;
vector<int> _cutPositions;
public:
DynamicProgramming(vector<int> price);
int RecursiveRodCutting(int length);
pair<int, vector<int>> DpGetMaximumProfitWithCuts(int length);
};
}
61 changes: 61 additions & 0 deletions source/0005_DynamicProgramming/0001_RodCutting.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "../../include/0005_DynamicProgramming/0001_RodCutting.h"
#include<iostream>
#include<vector>
using namespace std;

namespace RodCutting
{
DynamicProgramming::DynamicProgramming(vector<int> price)
{
this->_price = price;
this->_totalLength = this->_price.size();
}

int DynamicProgramming::RecursiveRodCutting(int length)
{
// Base case
if (length == 0)
{
return 0;
}

int result = 0;

for (int cut = 1; cut <= length; cut++)
{
result = max(result, this->_price[cut - 1] + this->RecursiveRodCutting(length - cut));
}

return result;
}

pair<int, vector<int>> DynamicProgramming::DpGetMaximumProfitWithCuts(int length)
{
vector<int> dp(this->_price.size() + 1, 0);
this->_cutPositions = vector<int>(this->_price.size() + 1, 0);

// Find maximum value for all rod of length i.
for (int i = 1; i <= this->_totalLength; i++)
{
for (int j = 1; j <= i; j++)
{
if (dp[i] < (this->_price[j - 1] + dp[i - j]))
{
dp[i] = this->_price[j - 1] + dp[i - j];
this->_cutPositions[i] = j - 1;
}
}
}

// Re-construct the cuts
vector<int> cutLengths;
int currentLength = length;
while (currentLength > 0)
{
cutLengths.push_back(this->_cutPositions[currentLength] + 1);
currentLength -= cutLengths.back();
}

return { dp[length] ,cutLengths };
}
}
8 changes: 8 additions & 0 deletions source/0005_DynamicProgramming/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Specify the source files
set(0005DYNAMICPROGRAMMING_SOURCES
0001_RodCutting.cc

)

# Create a library target
add_library(0005DYNAMICPROGRAMMING ${0005DYNAMICPROGRAMMING_SOURCES})
40 changes: 40 additions & 0 deletions test/0005_DynamicProgramming/0001_RodCuttingTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<gtest/gtest.h>
#include "../../include/0005_DynamicProgramming/0001_RodCutting.h"
#include "../0000_CommonUtilities/UnitTestHelper.h"

namespace RodCutting
{
UnitTestHelper unitTestHelper;

TEST(DpRodCutting, RecursiveTest)
{
// Arrange
vector<int> price = { 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };
int length = 8;
DynamicProgramming dp(price);
int expectedResult = 22;

// Act
int actualResult = dp.RecursiveRodCutting(length);

// Assert
ASSERT_EQ(actualResult, expectedResult);
}

TEST(DpRodCutting, DpTest)
{
// Arrange
vector<int> price = { 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };
int length = 8;
DynamicProgramming dp(price);
int expectedProfit = 22;
vector<int> expectedCuts = { 2, 6 };

// Act
pair<int, vector<int>> actualResult = dp.DpGetMaximumProfitWithCuts(length);

// Assert
ASSERT_EQ(actualResult.first, expectedProfit);
ASSERT_EQ(actualResult.second, expectedCuts);
}
}
33 changes: 33 additions & 0 deletions test/0005_DynamicProgramming/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_policy(SET CMP0135 NEW)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
0005DynamicProgrammingTests
0001_RodCuttingTest.cc

)

target_link_libraries(
0005DynamicProgrammingTests
GTest::gtest_main
0005DYNAMICPROGRAMMING
)

# Add .clang-tidy configuration to this library.
if(CLANG_TIDY_EXE)
set_target_properties(0005DYNAMICPROGRAMMING PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
endif()

include(GoogleTest)
gtest_discover_tests(0005DynamicProgrammingTests DISCOVERY_TIMEOUT 30)