Skip to content

Commit 184df9b

Browse files
authored
Merge pull request #102 from Debashis08/release
Release
2 parents a364744 + 97e8892 commit 184df9b

14 files changed

+649
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include<vector>
4+
using namespace std;
5+
6+
/*
7+
Pattern 1
8+
Linear Recurrence
9+
10+
Description
11+
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.
12+
13+
Examples:
14+
15+
Input : n = 3
16+
Output : 4
17+
Explanation:
18+
{1}, {2}, {3} : all single
19+
{1}, {2, 3} : 2 and 3 paired but 1 is single.
20+
{1, 2}, {3} : 1 and 2 are paired but 3 is single.
21+
{1, 3}, {2} : 1 and 3 are paired but 2 is single.
22+
Note that {1, 2} and {2, 1} are considered same.
23+
24+
Mathematical Explanation:
25+
The problem is simplified version of how many ways we can divide n elements into multiple groups.
26+
(here group size will be max of 2 elements).
27+
In case of n = 3, we have only 2 ways to make a group:
28+
1) all elements are individual(1,1,1)
29+
2) a pair and individual (2,1)
30+
In case of n = 4, we have 3 ways to form a group:
31+
1) all elements are individual (1,1,1,1)
32+
2) 2 individuals and one pair (2,1,1)
33+
3) 2 separate pairs (2,2)
34+
*/
35+
36+
namespace FriendsPairingProblem
37+
{
38+
class DynamicProgramming
39+
{
40+
private:
41+
int CountFriendsPairingsRecursiveHelper(int n);
42+
public:
43+
int RecursiveCountFriendsPairings(int n);
44+
int DpCountFriendsPairings(int n);
45+
};
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include<vector>
4+
using namespace std;
5+
6+
/*
7+
Pattern 1
8+
Linear Recurrence
9+
10+
Description
11+
Given a distance 'dist', count total number of ways to cover the distance with 1, 2 and 3 steps.
12+
13+
Examples:
14+
15+
Input: n = 3
16+
Output: 4
17+
Explanation: Below are the four ways
18+
=> 1 step + 1 step + 1 step
19+
=> 1 step + 2 step
20+
=> 2 step + 1 step
21+
=> 3 step
22+
23+
24+
25+
Input: n = 4
26+
Output: 7
27+
Explanation: Below are the four ways
28+
=> 1 step + 1 step + 1 step + 1 step
29+
=> 1 step + 2 step + 1 step
30+
=> 2 step + 1 step + 1 step
31+
=> 1 step + 1 step + 2 step
32+
=> 2 step + 2 step
33+
=> 3 step + 1 step
34+
=> 1 step + 3 step
35+
*/
36+
37+
namespace WaysToCoverDistance
38+
{
39+
class DynamicProgramming
40+
{
41+
private:
42+
int WaysToCoverDistanceRecursiveHelper(int dist);
43+
public:
44+
int RecursiveWaysToCoverDistance(int dist);
45+
int DpWaysToCoverDistance(int dist);
46+
};
47+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
#include<vector>
4+
using namespace std;
5+
6+
/*
7+
Pattern 1
8+
Linear Recurrence
9+
10+
Description
11+
There are n stairs, and a person standing at the bottom wants to climb stairs to reach the top.
12+
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.
13+
14+
Note: This problem is similar to Count ways to reach Nth stair (Order does not matter) with the only difference that in this problem,
15+
we count all distinct ways where different orderings of the steps are considered unique.
16+
17+
Examples:
18+
19+
Input: n = 1
20+
Output: 1
21+
Explanation: There is only one way to climb 1 stair.
22+
23+
Input: n = 2
24+
Output: 2
25+
Explanation: There are two ways to reach 2th stair: {1, 1} and {2}.
26+
27+
Input: n = 4
28+
Output: 5
29+
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}.
30+
*/
31+
32+
namespace CountWaysToReachNthStairIncludeOrder
33+
{
34+
class DynamicProgramming
35+
{
36+
private:
37+
int RecursiveCountWaysToReachNthStairIncludeOrderHelper(int n);
38+
public:
39+
int RecursiveCountWaysToReachNthStairIncludeOrder(int n);
40+
int DpCountWaysToReachNthStairIncludeOrder(int n);
41+
};
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include<vector>
4+
using namespace std;
5+
6+
/*
7+
Pattern 1
8+
Linear Recurrence
9+
10+
Description
11+
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).
12+
13+
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.
14+
15+
Examples:
16+
17+
Input: n = 1
18+
Output: 1
19+
Explanation: There is only one way to climb 1 stair.
20+
21+
Input: n = 2
22+
Output: 2
23+
Explanation: There are two ways to climb 2 stairs: {1, 1} and {2}.
24+
25+
Input: n = 4
26+
Output: 3
27+
Explanation: Three ways to reach 4th stair: {1, 1, 1, 1}, {1, 1, 2} and {2, 2}.
28+
29+
Input: n = 5
30+
Output: 3
31+
Explanation: Three ways to reach 5th stair: {1, 1, 1, 1, 1}, {1, 1, 1, 2} and {1, 2, 2}.
32+
*/
33+
34+
namespace CountWaysToReachNthStairExcludeOrder
35+
{
36+
class DynamicProgramming
37+
{
38+
private:
39+
int RecursiveCountWaysToReachNthStairExcludeOrderHelper(int n);
40+
public:
41+
int RecursiveCountWaysToReachNthStairExcludeOrder(int n);
42+
int DpCountWaysToReachNthStairExcludeOrder(int n);
43+
};
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "../../include/0005_DynamicProgramming/0009_FriendsPairingProblem.h"
2+
3+
namespace FriendsPairingProblem
4+
{
5+
// Dynamic Programming Private Member Methods.
6+
int DynamicProgramming::CountFriendsPairingsRecursiveHelper(int n)
7+
{
8+
if (n <= 1)
9+
{
10+
return 1;
11+
}
12+
int result = 0;
13+
result += this->CountFriendsPairingsRecursiveHelper(n - 1);
14+
result += (n - 1) * this->CountFriendsPairingsRecursiveHelper(n - 2);
15+
16+
return result;
17+
}
18+
19+
// Dynamic Programming Public Member Methods.
20+
int DynamicProgramming::RecursiveCountFriendsPairings(int n)
21+
{
22+
return this->CountFriendsPairingsRecursiveHelper(n);
23+
}
24+
25+
int DynamicProgramming::DpCountFriendsPairings(int n)
26+
{
27+
vector<int> dp(n + 1, 0);
28+
dp[0] = 0;
29+
dp[1] = 1;
30+
dp[2] = 2;
31+
32+
for (int i = 3; i <= n; i++)
33+
{
34+
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
35+
}
36+
37+
return dp[n];
38+
}
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "../../include/0005_DynamicProgramming/0010_WaysToCoverDistance.h"
2+
3+
namespace WaysToCoverDistance
4+
{
5+
// Dynamic Programming Private Member Methods.
6+
int DynamicProgramming::WaysToCoverDistanceRecursiveHelper(int dist)
7+
{
8+
if (dist < 0)
9+
{
10+
return 0;
11+
}
12+
13+
if (dist == 0)
14+
{
15+
return 1;
16+
}
17+
18+
int result = 0;
19+
result += this->WaysToCoverDistanceRecursiveHelper(dist - 1);
20+
result += this->WaysToCoverDistanceRecursiveHelper(dist - 2);
21+
result += this->WaysToCoverDistanceRecursiveHelper(dist - 3);
22+
23+
return result;
24+
}
25+
26+
// Dynamic Programming Public Member Methods.
27+
int DynamicProgramming::RecursiveWaysToCoverDistance(int dist)
28+
{
29+
return this->WaysToCoverDistanceRecursiveHelper(dist);
30+
}
31+
32+
int DynamicProgramming::DpWaysToCoverDistance(int dist)
33+
{
34+
vector<int> dp(dist + 1, 0);
35+
dp[0] = 1;
36+
if (dist >= 1)
37+
{
38+
dp[1] = 1;
39+
}
40+
if (dist >= 2)
41+
{
42+
dp[2] = 2;
43+
}
44+
45+
for(int i = 3; i <= dist; i++)
46+
{
47+
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
48+
}
49+
50+
return dp[dist];
51+
}
52+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "../../include/0005_DynamicProgramming/0011_CountWaysToReachNthStairIncludeOrder.h"
2+
3+
namespace CountWaysToReachNthStairIncludeOrder
4+
{
5+
// Dynamic Programming Private Member Methods.
6+
int DynamicProgramming::RecursiveCountWaysToReachNthStairIncludeOrderHelper(int n)
7+
{
8+
if (n < 0)
9+
{
10+
return 0;
11+
}
12+
if (n == 0 || n == 1)
13+
{
14+
return 1;
15+
}
16+
17+
int result = 0;
18+
result += this->RecursiveCountWaysToReachNthStairIncludeOrderHelper(n - 1);
19+
result += this->RecursiveCountWaysToReachNthStairIncludeOrderHelper(n - 2);
20+
21+
return result;
22+
}
23+
24+
// Dynamic Programming Public Member Methods.
25+
int DynamicProgramming::RecursiveCountWaysToReachNthStairIncludeOrder(int n)
26+
{
27+
return this->RecursiveCountWaysToReachNthStairIncludeOrderHelper(n);
28+
}
29+
30+
int DynamicProgramming::DpCountWaysToReachNthStairIncludeOrder(int n)
31+
{
32+
vector<int> dp(n + 1, 0);
33+
dp[0] = 1;
34+
if (n >= 1)
35+
{
36+
dp[1] = 1;
37+
}
38+
for(int i = 2; i <= n; i++)
39+
{
40+
dp[i] = dp[i - 1] + dp[i - 2];
41+
}
42+
return dp[n];
43+
}
44+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "../../include/0005_DynamicProgramming/0012_CountWaysToReachNthStairExcludeOrder.h"
2+
3+
namespace CountWaysToReachNthStairExcludeOrder
4+
{
5+
// notes:
6+
/*
7+
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.
8+
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.
9+
A person can reach nth stair from either(n - 1)th stair or from(n - 2)th stair.So, there are two cases :
10+
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.
11+
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.
12+
Therefore the Recurrence relation will be :
13+
14+
nthStair(n) = 1 (last step was of size 1) + nthStair(n - 2) (last step was of size 2)
15+
so f(n) = 1 + f(n - 2)
16+
*/
17+
18+
// Dynamic Programming Private Member Methods.
19+
int DynamicProgramming::RecursiveCountWaysToReachNthStairExcludeOrderHelper(int n)
20+
{
21+
if (n < 0)
22+
{
23+
return 0;
24+
}
25+
if (n == 0)
26+
{
27+
return 1;
28+
}
29+
30+
return 1 + this->RecursiveCountWaysToReachNthStairExcludeOrderHelper(n - 2);
31+
}
32+
33+
// Dynamic Programming Public Member Methods.
34+
int DynamicProgramming::RecursiveCountWaysToReachNthStairExcludeOrder(int n)
35+
{
36+
return this->RecursiveCountWaysToReachNthStairExcludeOrderHelper(n);
37+
}
38+
39+
int DynamicProgramming::DpCountWaysToReachNthStairExcludeOrder(int n)
40+
{
41+
vector<int> dp(n + 1, 0);
42+
dp[0] = 1;
43+
if (n >= 1)
44+
{
45+
dp[1] = 1;
46+
}
47+
for (int i = 2; i <= n; i++)
48+
{
49+
dp[i] = 1 + dp[i - 2];
50+
}
51+
return dp[n];
52+
}
53+
}

source/0005_DynamicProgramming/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ set(0005DYNAMICPROGRAMMING_SOURCES
88
0006_HouseRobber2.cc
99
0007_DecodeWays.cc
1010
0008_TilingProblem.cc
11+
0009_FriendsPairingProblem.cc
12+
0010_WaysToCoverDistance.cc
13+
0011_CountWaysToReachNthStairIncludeOrder.cc
14+
0012_CountWaysToReachNthStairExcludeOrder.cc
1115

1216
)
1317

0 commit comments

Comments
 (0)