Skip to content

Commit 93da32a

Browse files
authored
Merge pull request #88 from Debashis08/feature-dp-implementation
feature: dp pattern 1 fibonacci, tribonacci added
2 parents 94caa26 + addca6e commit 93da32a

14 files changed

+414
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include<vector>
3+
using namespace std;
4+
5+
/*
6+
Pattern 1
7+
Linear Recurrence
8+
9+
Description
10+
Print the n'th Fibonacci number.
11+
12+
*/
13+
14+
namespace FibonacciNumber
15+
{
16+
class DynamicProgramming
17+
{
18+
private:
19+
public:
20+
int RecursiveNthFibonacci(int n);
21+
int DpNthFibonacci(int n);
22+
};
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include<vector>
3+
using namespace std;
4+
5+
/*
6+
Pattern 1
7+
Linear Recurrence
8+
9+
Description
10+
Print the n'th Tribonacci number.
11+
12+
*/
13+
14+
namespace TribonacciNumber
15+
{
16+
class DynamicProgramming
17+
{
18+
private:
19+
public:
20+
int RecursiveNthTribonacci(int n);
21+
int DpNthTribonacci(int n);
22+
};
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#include<vector>
3+
using namespace std;
4+
5+
/*
6+
Pattern 1
7+
Linear Recurrence
8+
9+
Description
10+
There are n stairs, and a person standing at the bottom wants to climb stairs to reach the top.
11+
The person can climb either 1 stair or 2 stairs at a time, the task is to count the number of ways that a person can reach at the top.
12+
13+
*/
14+
15+
namespace ClimbingStairs
16+
{
17+
class DynamicProgramming
18+
{
19+
private:
20+
public:
21+
int RecursiveCountWays(int n);
22+
int DpCountWays(int n);
23+
};
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include<vector>
3+
using namespace std;
4+
5+
/*
6+
Pattern 1
7+
Linear Recurrence
8+
9+
Description
10+
Given an array of integers cost[] of length n, where cost[i] is the cost of the ith step on a staircase. Once the cost is paid, we can either climb 1 or 2 steps.
11+
We can either start from the step with index 0, or the step with index 1. The task is to find the minimum cost to reach the top.
12+
13+
*/
14+
15+
namespace MinimumCostClimbingStairs
16+
{
17+
class DynamicProgramming
18+
{
19+
private:
20+
int MinCostRecursive(int step, vector<int>& cost);
21+
public:
22+
int RecursiveMinimumCostClimbingStairs(vector<int>& cost);
23+
int DpMinimumCostClimbingStairs(vector<int>& cost);
24+
};
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "../../include/0005_DynamicProgramming/0001_FibonacciNumber.h"
2+
3+
namespace FibonacciNumber
4+
{
5+
int DynamicProgramming::RecursiveNthFibonacci(int n)
6+
{
7+
if (n <= 1)
8+
{
9+
return n;
10+
}
11+
12+
return this->RecursiveNthFibonacci(n - 1) + this->RecursiveNthFibonacci(n - 2);
13+
}
14+
15+
int DynamicProgramming::DpNthFibonacci(int n)
16+
{
17+
vector<int> dp(n + 1, 0);
18+
dp[0] = 0;
19+
dp[1] = 1;
20+
21+
for (int i = 2; i <= n; i++)
22+
{
23+
dp[i] = dp[i - 1] + dp[i - 2];
24+
}
25+
26+
return dp[n];
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "../../include/0005_DynamicProgramming/0002_TribonacciNumber.h"
2+
3+
namespace TribonacciNumber
4+
{
5+
int DynamicProgramming::RecursiveNthTribonacci(int n)
6+
{
7+
if (n == 0 || n == 1 || n == 2)
8+
{
9+
return 0;
10+
}
11+
12+
if (n == 3)
13+
{
14+
return 1;
15+
}
16+
17+
return this->RecursiveNthTribonacci(n - 1) + this->RecursiveNthTribonacci(n - 2) + this->RecursiveNthTribonacci(n - 3);
18+
}
19+
20+
int DynamicProgramming::DpNthTribonacci(int n)
21+
{
22+
vector<int> dp(n, 0);
23+
dp[0] = dp[1] = 0;
24+
dp[2] = 1;
25+
26+
for (int i = 3; i < n; i++)
27+
{
28+
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
29+
}
30+
31+
return dp[n - 1];
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "../../include/0005_DynamicProgramming/0003_ClimbingStairs.h"
2+
using namespace std;
3+
4+
namespace ClimbingStairs
5+
{
6+
int DynamicProgramming::RecursiveCountWays(int n)
7+
{
8+
if (n == 0 || n == 1)
9+
{
10+
return 1;
11+
}
12+
13+
return this->RecursiveCountWays(n - 1) + this->RecursiveCountWays(n - 2);
14+
}
15+
16+
int DynamicProgramming::DpCountWays(int n)
17+
{
18+
vector<int> dp(n + 1, 0);
19+
dp[0] = 1;
20+
dp[1] = 1;
21+
22+
for (int i = 2; i <= n; i++)
23+
{
24+
dp[i] = dp[i - 1] + dp[i - 2];
25+
}
26+
27+
return dp[n];
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "../../include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h"
2+
#include<algorithm>
3+
4+
namespace MinimumCostClimbingStairs
5+
{
6+
int DynamicProgramming::MinCostRecursive(int step, vector<int>& cost)
7+
{
8+
if (step == 0 || step == 1)
9+
{
10+
return cost[step];
11+
}
12+
13+
return cost[step] + min(this->MinCostRecursive(step - 1, cost), this->MinCostRecursive(step - 2, cost));
14+
}
15+
16+
int DynamicProgramming::RecursiveMinimumCostClimbingStairs(vector<int>& cost)
17+
{
18+
int totalSteps = cost.size();
19+
20+
if (totalSteps == 1)
21+
{
22+
return cost[0];
23+
}
24+
25+
return min(this->MinCostRecursive(totalSteps - 1, cost), this->MinCostRecursive(totalSteps - 2, cost));
26+
}
27+
28+
int DynamicProgramming::DpMinimumCostClimbingStairs(vector<int>& cost)
29+
{
30+
int totalSteps = cost.size();
31+
vector<int> dp(totalSteps, 0);
32+
33+
if (totalSteps == 1)
34+
{
35+
return cost[0];
36+
}
37+
38+
dp[0] = cost[0];
39+
dp[1] = cost[1];
40+
41+
for (int i = 2; i < totalSteps; i++)
42+
{
43+
dp[i] = cost[i] + min(dp[i - 1], dp[i - 2]);
44+
}
45+
46+
return min(dp[totalSteps - 1], dp[totalSteps - 2]);
47+
}
48+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Specify the source files
2+
set(0005DYNAMICPROGRAMMING_SOURCES
3+
0001_FibonacciNumber.cc
4+
0002_TribonacciNumber.cc
5+
0003_ClimbingStairs.cc
6+
0004_MinimumCostClimbingStairs.cc
7+
8+
)
9+
10+
# Create a library target
11+
add_library(0005DYNAMICPROGRAMMING ${0005DYNAMICPROGRAMMING_SOURCES})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<gtest/gtest.h>
2+
#include "../../include/0005_DynamicProgramming/0001_FibonacciNumber.h"
3+
using namespace std;
4+
5+
namespace FibonacciNumber
6+
{
7+
TEST(FibonacciNumber, RecursiveTest)
8+
{
9+
// Arrange
10+
DynamicProgramming dp;
11+
int n = 5;
12+
int expectedFib = 5;
13+
14+
// Act
15+
int actualFib = dp.RecursiveNthFibonacci(n);
16+
17+
// Assert
18+
ASSERT_EQ(expectedFib, actualFib);
19+
}
20+
21+
TEST(FibonacciNumber, DpTest)
22+
{
23+
// Arrange
24+
DynamicProgramming dp;
25+
int n = 5;
26+
int expectedFib = 5;
27+
28+
// Act
29+
int actualFib = dp.DpNthFibonacci(n);
30+
31+
// Assert
32+
ASSERT_EQ(expectedFib, actualFib);
33+
}
34+
}

0 commit comments

Comments
 (0)