Skip to content

Commit 57ba98e

Browse files
authored
Merge pull request #92 from Debashis08/release
Release
2 parents 94caa26 + 7c93cb5 commit 57ba98e

23 files changed

+833
-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(size_t step, vector<int>& cost);
21+
public:
22+
int RecursiveMinimumCostClimbingStairs(vector<int>& cost);
23+
int DpMinimumCostClimbingStairs(vector<int>& cost);
24+
};
25+
}
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+
There are n houses built in a line, each of which contains some money in it.
11+
A robber wants to steal money from these houses, but he can’t steal from two adjacent houses. The task is to find the maximum amount of money which can be stolen.
12+
13+
*/
14+
15+
namespace HouseRobber1
16+
{
17+
class DynamicProgramming
18+
{
19+
private:
20+
int MaxLootRecursive(size_t house, vector<int>& houseValues);
21+
public:
22+
int RecursiveMaximumLoot(vector<int>& houseValues);
23+
int DpMaximumLoot(vector<int>& houseValues);
24+
};
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
#include<vector>
3+
using namespace std;
4+
5+
/*
6+
Pattern 1
7+
Linear Recurrence
8+
9+
Description
10+
You are given an array arr[] which represents houses arranged in a circle, where each house has a certain value. A thief aims to maximize the total stolen value without robbing two adjacent houses.
11+
Determine the maximum amount the thief can steal.
12+
13+
Note: Since the houses are in a circle, the first and last houses are also considered adjacent.
14+
15+
*/
16+
17+
namespace HouseRobber2
18+
{
19+
class DynamicProgramming
20+
{
21+
private:
22+
int MaxLootRecursive(size_t house, vector<int>& houseValues);
23+
int MaxLootDp(size_t firstHouse, size_t lastHouse, vector<int>& houseValues);
24+
public:
25+
int RecursiveMaximumLoot(vector<int>& houseValues);
26+
int DpMaximumLoot(vector<int>& houseValues);
27+
};
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include<vector>
4+
#include<string>
5+
using namespace std;
6+
7+
/*
8+
Pattern 1
9+
Linear Recurrence
10+
11+
Description
12+
Let 1 maps to 'A', 2 maps to 'B', ..., 26 to 'Z'.Given a digit sequence, count the number of possible decodings of the given digit sequence.
13+
14+
Consider the input string "123".There are three valid ways to decode it :
15+
"ABC" : The grouping is(1, 2, 3) -> 'A', 'B', 'C'
16+
"AW" : The grouping is(1, 23) -> 'A', 'W'
17+
"LC" : The grouping is(12, 3) -> 'L', 'C'
18+
Note : Groupings that contain invalid codes(e.g., "0" by itself or numbers greater than "26") are not allowed.
19+
For instance, the string "230" is invalid because "0" cannot stand alone, and "30" is greater than "26", so it cannot represent any letter.The task is to find the total number of valid ways to decode a given string.
20+
*/
21+
22+
namespace DecodeWays
23+
{
24+
class DynamicProgramming
25+
{
26+
private:
27+
int CountWaysRecursiveHelper(string& digits, size_t index);
28+
public:
29+
int RecursiveCountWays(string digits);
30+
int DpCountways(string digits);
31+
};
32+
}
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+
}

0 commit comments

Comments
 (0)