Skip to content
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
46 changes: 46 additions & 0 deletions include/0005_DynamicProgramming/0009_FriendsPairingProblem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include<vector>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up.

Examples:

Input : n = 3
Output : 4
Explanation:
{1}, {2}, {3} : all single
{1}, {2, 3} : 2 and 3 paired but 1 is single.
{1, 2}, {3} : 1 and 2 are paired but 3 is single.
{1, 3}, {2} : 1 and 3 are paired but 2 is single.
Note that {1, 2} and {2, 1} are considered same.

Mathematical Explanation:
The problem is simplified version of how many ways we can divide n elements into multiple groups.
(here group size will be max of 2 elements).
In case of n = 3, we have only 2 ways to make a group:
1) all elements are individual(1,1,1)
2) a pair and individual (2,1)
In case of n = 4, we have 3 ways to form a group:
1) all elements are individual (1,1,1,1)
2) 2 individuals and one pair (2,1,1)
3) 2 separate pairs (2,2)
*/

namespace FriendsPairingProblem
{
class DynamicProgramming
{
private:
int CountFriendsPairingsRecursiveHelper(int n);
public:
int RecursiveCountFriendsPairings(int n);
int DpCountFriendsPairings(int n);
};
}
47 changes: 47 additions & 0 deletions include/0005_DynamicProgramming/0010_WaysToCoverDistance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include<vector>
using namespace std;

/*
Pattern 1
Linear Recurrence

Description
Given a distance 'dist', count total number of ways to cover the distance with 1, 2 and 3 steps.

Examples:

Input: n = 3
Output: 4
Explanation: Below are the four ways
=> 1 step + 1 step + 1 step
=> 1 step + 2 step
=> 2 step + 1 step
=> 3 step



Input: n = 4
Output: 7
Explanation: Below are the four ways
=> 1 step + 1 step + 1 step + 1 step
=> 1 step + 2 step + 1 step
=> 2 step + 1 step + 1 step
=> 1 step + 1 step + 2 step
=> 2 step + 2 step
=> 3 step + 1 step
=> 1 step + 3 step
*/

namespace WaysToCoverDistance
{
class DynamicProgramming
{
private:
int WaysToCoverDistanceRecursiveHelper(int dist);
public:
int RecursiveWaysToCoverDistance(int dist);
int DpWaysToCoverDistance(int dist);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#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.

Note: This problem is similar to Count ways to reach Nth stair (Order does not matter) with the only difference that in this problem,
we count all distinct ways where different orderings of the steps are considered unique.

Examples:

Input: n = 1
Output: 1
Explanation: There is only one way to climb 1 stair.

Input: n = 2
Output: 2
Explanation: There are two ways to reach 2th stair: {1, 1} and {2}.

Input: n = 4
Output: 5
Explanation: There are five ways to reach 4th stair: {1, 1, 1, 1}, {1, 1, 2}, {2, 1, 1}, {1, 2, 1} and {2, 2}.
*/

namespace CountWaysToReachNthStairIncludeOrder
{
class DynamicProgramming
{
private:
int RecursiveCountWaysToReachNthStairIncludeOrderHelper(int n);
public:
int RecursiveCountWaysToReachNthStairIncludeOrder(int n);
int DpCountWaysToReachNthStairIncludeOrder(int n);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#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 reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does not matter).

Note: The problem is similar to Climbing Stairs - Count ways to reach Nth stair with the only difference that in this problem, we don't have to count those ways which only differ in ordering of the steps.

Examples:

Input: n = 1
Output: 1
Explanation: There is only one way to climb 1 stair.

Input: n = 2
Output: 2
Explanation: There are two ways to climb 2 stairs: {1, 1} and {2}.

Input: n = 4
Output: 3
Explanation: Three ways to reach 4th stair: {1, 1, 1, 1}, {1, 1, 2} and {2, 2}.

Input: n = 5
Output: 3
Explanation: Three ways to reach 5th stair: {1, 1, 1, 1, 1}, {1, 1, 1, 2} and {1, 2, 2}.
*/

namespace CountWaysToReachNthStairExcludeOrder
{
class DynamicProgramming
{
private:
int RecursiveCountWaysToReachNthStairExcludeOrderHelper(int n);
public:
int RecursiveCountWaysToReachNthStairExcludeOrder(int n);
int DpCountWaysToReachNthStairExcludeOrder(int n);
};
}
39 changes: 39 additions & 0 deletions source/0005_DynamicProgramming/0009_FriendsPairingProblem.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "../../include/0005_DynamicProgramming/0009_FriendsPairingProblem.h"

namespace FriendsPairingProblem
{
// Dynamic Programming Private Member Methods.
int DynamicProgramming::CountFriendsPairingsRecursiveHelper(int n)
{
if (n <= 1)
{
return 1;
}
int result = 0;
result += this->CountFriendsPairingsRecursiveHelper(n - 1);
result += (n - 1) * this->CountFriendsPairingsRecursiveHelper(n - 2);

return result;
}

// Dynamic Programming Public Member Methods.
int DynamicProgramming::RecursiveCountFriendsPairings(int n)
{
return this->CountFriendsPairingsRecursiveHelper(n);
}

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

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

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

namespace WaysToCoverDistance
{
// Dynamic Programming Private Member Methods.
int DynamicProgramming::WaysToCoverDistanceRecursiveHelper(int dist)
{
if (dist < 0)
{
return 0;
}

if (dist == 0)
{
return 1;
}

int result = 0;
result += this->WaysToCoverDistanceRecursiveHelper(dist - 1);
result += this->WaysToCoverDistanceRecursiveHelper(dist - 2);
result += this->WaysToCoverDistanceRecursiveHelper(dist - 3);

return result;
}

// Dynamic Programming Public Member Methods.
int DynamicProgramming::RecursiveWaysToCoverDistance(int dist)
{
return this->WaysToCoverDistanceRecursiveHelper(dist);
}

int DynamicProgramming::DpWaysToCoverDistance(int dist)
{
vector<int> dp(dist + 1, 0);
dp[0] = 1;
if (dist >= 1)
{
dp[1] = 1;
}
if (dist >= 2)
{
dp[2] = 2;
}

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

return dp[dist];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "../../include/0005_DynamicProgramming/0011_CountWaysToReachNthStairIncludeOrder.h"

namespace CountWaysToReachNthStairIncludeOrder
{
// Dynamic Programming Private Member Methods.
int DynamicProgramming::RecursiveCountWaysToReachNthStairIncludeOrderHelper(int n)
{
if (n < 0)
{
return 0;
}
if (n == 0 || n == 1)
{
return 1;
}

int result = 0;
result += this->RecursiveCountWaysToReachNthStairIncludeOrderHelper(n - 1);
result += this->RecursiveCountWaysToReachNthStairIncludeOrderHelper(n - 2);

return result;
}

// Dynamic Programming Public Member Methods.
int DynamicProgramming::RecursiveCountWaysToReachNthStairIncludeOrder(int n)
{
return this->RecursiveCountWaysToReachNthStairIncludeOrderHelper(n);
}

int DynamicProgramming::DpCountWaysToReachNthStairIncludeOrder(int n)
{
vector<int> dp(n + 1, 0);
dp[0] = 1;
if (n >= 1)
{
dp[1] = 1;
}
for(int i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "../../include/0005_DynamicProgramming/0012_CountWaysToReachNthStairExcludeOrder.h"

namespace CountWaysToReachNthStairExcludeOrder
{
// notes:
/*
To avoid counting ways which only differ in order, we can assume that a person initially takes only steps of size 1 followed by steps of size 2.
In other words, once a person takes a step of size 2, he will continue taking steps of size 2 till he reaches the nth stair.
A person can reach nth stair from either(n - 1)th stair or from(n - 2)th stair.So, there are two cases :
The person has reached nth step from(n - 1)th step, this means that the last step was of size 1 and all the previous steps should also be of size 1. So, there is only 1 way.
The person has reached nth step from(n - 2)th step, this means that the last step was of size 2 and the previous steps can either be of size 1 or size 2.
Therefore the Recurrence relation will be :

nthStair(n) = 1 (last step was of size 1) + nthStair(n - 2) (last step was of size 2)
so f(n) = 1 + f(n - 2)
*/

// Dynamic Programming Private Member Methods.
int DynamicProgramming::RecursiveCountWaysToReachNthStairExcludeOrderHelper(int n)
{
if (n < 0)
{
return 0;
}
if (n == 0)
{
return 1;
}

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

// Dynamic Programming Public Member Methods.
int DynamicProgramming::RecursiveCountWaysToReachNthStairExcludeOrder(int n)
{
return this->RecursiveCountWaysToReachNthStairExcludeOrderHelper(n);
}

int DynamicProgramming::DpCountWaysToReachNthStairExcludeOrder(int n)
{
vector<int> dp(n + 1, 0);
dp[0] = 1;
if (n >= 1)
{
dp[1] = 1;
}
for (int i = 2; i <= n; i++)
{
dp[i] = 1 + dp[i - 2];
}
return dp[n];
}
}
4 changes: 4 additions & 0 deletions source/0005_DynamicProgramming/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ set(0005DYNAMICPROGRAMMING_SOURCES
0006_HouseRobber2.cc
0007_DecodeWays.cc
0008_TilingProblem.cc
0009_FriendsPairingProblem.cc
0010_WaysToCoverDistance.cc
0011_CountWaysToReachNthStairIncludeOrder.cc
0012_CountWaysToReachNthStairExcludeOrder.cc

)

Expand Down
Loading