Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions include/0005_DynamicProgramming/0001_FibonacciNumber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include<vector>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
Print the n'th Fibonacci number.

*/

namespace FibonacciNumber
{
class DynamicProgramming
{
private:
public:
int RecursiveNthFibonacci(int n);
int DpNthFibonacci(int n);
};
}
23 changes: 23 additions & 0 deletions include/0005_DynamicProgramming/0002_TribonacciNumber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include<vector>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
Print the n'th Tribonacci number.

*/

namespace TribonacciNumber
{
class DynamicProgramming
{
private:
public:
int RecursiveNthTribonacci(int n);
int DpNthTribonacci(int n);
};
}
24 changes: 24 additions & 0 deletions include/0005_DynamicProgramming/0003_ClimbingStairs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include<vector>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
There are n stairs, and a person standing at the bottom wants to climb stairs to reach the top.
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.

*/

namespace ClimbingStairs
{
class DynamicProgramming
{
private:
public:
int RecursiveCountWays(int n);
int DpCountWays(int n);
};
}
25 changes: 25 additions & 0 deletions include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include<vector>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
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.
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.

*/

namespace MinimumCostClimbingStairs
{
class DynamicProgramming
{
private:
int MinCostRecursive(size_t step, vector<int>& cost);
public:
int RecursiveMinimumCostClimbingStairs(vector<int>& cost);
int DpMinimumCostClimbingStairs(vector<int>& cost);
};
}
25 changes: 25 additions & 0 deletions include/0005_DynamicProgramming/0005_HouseRobber1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include<vector>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
There are n houses built in a line, each of which contains some money in it.
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.

*/

namespace HouseRobber1
{
class DynamicProgramming
{
private:
int MaxLootRecursive(size_t house, vector<int>& houseValues);
public:
int RecursiveMaximumLoot(vector<int>& houseValues);
int DpMaximumLoot(vector<int>& houseValues);
};
}
28 changes: 28 additions & 0 deletions include/0005_DynamicProgramming/0006_HouseRobber2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include<vector>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
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.
Determine the maximum amount the thief can steal.

Note: Since the houses are in a circle, the first and last houses are also considered adjacent.

*/

namespace HouseRobber2
{
class DynamicProgramming
{
private:
int MaxLootRecursive(size_t house, vector<int>& houseValues);
int MaxLootDp(size_t firstHouse, size_t lastHouse, vector<int>& houseValues);
public:
int RecursiveMaximumLoot(vector<int>& houseValues);
int DpMaximumLoot(vector<int>& houseValues);
};
}
32 changes: 32 additions & 0 deletions include/0005_DynamicProgramming/0007_DecodeWays.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include<vector>
#include<string>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
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.

Consider the input string "123".There are three valid ways to decode it :
"ABC" : The grouping is(1, 2, 3) -> 'A', 'B', 'C'
"AW" : The grouping is(1, 23) -> 'A', 'W'
"LC" : The grouping is(12, 3) -> 'L', 'C'
Note : Groupings that contain invalid codes(e.g., "0" by itself or numbers greater than "26") are not allowed.
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.
*/

namespace DecodeWays
{
class DynamicProgramming
{
private:
int CountWaysRecursiveHelper(string& digits, size_t index);
public:
int RecursiveCountWays(string digits);
int DpCountways(string digits);
};
}
28 changes: 28 additions & 0 deletions source/0005_DynamicProgramming/0001_FibonacciNumber.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "../../include/0005_DynamicProgramming/0001_FibonacciNumber.h"

namespace FibonacciNumber
{
int DynamicProgramming::RecursiveNthFibonacci(int n)
{
if (n <= 1)
{
return n;
}

return this->RecursiveNthFibonacci(n - 1) + this->RecursiveNthFibonacci(n - 2);
}

int DynamicProgramming::DpNthFibonacci(int n)
{
vector<int> dp(n + 1, 0);
dp[0] = 0;
dp[1] = 1;

for (int i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}

return dp[n];
}
}
33 changes: 33 additions & 0 deletions source/0005_DynamicProgramming/0002_TribonacciNumber.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "../../include/0005_DynamicProgramming/0002_TribonacciNumber.h"

namespace TribonacciNumber
{
int DynamicProgramming::RecursiveNthTribonacci(int n)
{
if (n == 0 || n == 1 || n == 2)
{
return 0;
}

if (n == 3)
{
return 1;
}

return this->RecursiveNthTribonacci(n - 1) + this->RecursiveNthTribonacci(n - 2) + this->RecursiveNthTribonacci(n - 3);
}

int DynamicProgramming::DpNthTribonacci(int n)
{
vector<int> dp(n, 0);
dp[0] = dp[1] = 0;
dp[2] = 1;

for (int i = 3; i < n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
}

return dp[n - 1];
}
}
29 changes: 29 additions & 0 deletions source/0005_DynamicProgramming/0003_ClimbingStairs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "../../include/0005_DynamicProgramming/0003_ClimbingStairs.h"
using namespace std;

namespace ClimbingStairs
{
int DynamicProgramming::RecursiveCountWays(int n)
{
if (n == 0 || n == 1)
{
return 1;
}

return this->RecursiveCountWays(n - 1) + this->RecursiveCountWays(n - 2);
}

int DynamicProgramming::DpCountWays(int n)
{
vector<int> dp(n + 1, 0);
dp[0] = 1;
dp[1] = 1;

for (int i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}

return dp[n];
}
}
48 changes: 48 additions & 0 deletions source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "../../include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h"
#include<algorithm>

namespace MinimumCostClimbingStairs
{
int DynamicProgramming::MinCostRecursive(size_t step, vector<int>& cost)
{
if (step == 0 || step == 1)
{
return cost[step];
}

return cost[step] + min(this->MinCostRecursive(step - 1, cost), this->MinCostRecursive(step - 2, cost));
}

int DynamicProgramming::RecursiveMinimumCostClimbingStairs(vector<int>& cost)
{
size_t totalSteps = cost.size();

if (totalSteps == 1)
{
return cost[0];
}

return min(this->MinCostRecursive(totalSteps - 1, cost), this->MinCostRecursive(totalSteps - 2, cost));
}

int DynamicProgramming::DpMinimumCostClimbingStairs(vector<int>& cost)
{
size_t totalSteps = cost.size();
vector<int> dp(totalSteps, 0);

if (totalSteps == 1)
{
return cost[0];
}

dp[0] = cost[0];
dp[1] = cost[1];

for (size_t i = 2; i < totalSteps; i++)
{
dp[i] = cost[i] + min(dp[i - 1], dp[i - 2]);
}

return min(dp[totalSteps - 1], dp[totalSteps - 2]);
}
}
44 changes: 44 additions & 0 deletions source/0005_DynamicProgramming/0005_HouseRobber1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "../../include/0005_DynamicProgramming/0005_HouseRobber1.h"

namespace HouseRobber1
{
int DynamicProgramming::MaxLootRecursive(size_t house, vector<int>& houseValues)
{
if (house <= 0)
{
return 0;
}

if (house == 1)
{
return houseValues[0];
}

int pickCurrentHouse = houseValues[house - 1] + this->MaxLootRecursive(house - 2, houseValues);
int dropCurrentHouse = this->MaxLootRecursive(house - 1, houseValues);

return max(pickCurrentHouse, dropCurrentHouse);
}

int DynamicProgramming::RecursiveMaximumLoot(vector<int>& houseValues)
{
size_t totalNumberOfHouses = houseValues.size();
return this->MaxLootRecursive(totalNumberOfHouses, houseValues);
}

int DynamicProgramming::DpMaximumLoot(vector<int>& houseValues)
{
size_t totalNumberOfHouses = houseValues.size();
vector<int> dp(totalNumberOfHouses + 1, 0);

dp[0] = 0;
dp[1] = houseValues[0];

for (size_t i = 2; i <= totalNumberOfHouses; i++)
{
dp[i] = max(dp[i - 2] + houseValues[i - 1], dp[i - 1]);
}

return dp[totalNumberOfHouses];
}
}
Loading