Skip to content

Commit d8426ec

Browse files
committed
feature: count ways to reach nth stair exclude order sol added
1 parent f05d37a commit d8426ec

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
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: 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(0005DYNAMICPROGRAMMING_SOURCES
1111
0009_FriendsPairingProblem.cc
1212
0010_WaysToCoverDistance.cc
1313
0011_CountWaysToReachNthStairIncludeOrder.cc
14+
0012_CountWaysToReachNthStairExcludeOrder.cc
1415

1516
)
1617

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<gtest/gtest.h>
2+
#include "../../include/0005_DynamicProgramming/0012_CountWaysToReachNthStairExcludeOrder.h"
3+
4+
namespace CountWaysToReachNthStairExcludeOrder
5+
{
6+
TEST(CountWaysToReachNthStairExcludeOrderTest, RecursiveCountWaysToReachNthStairExcludeOrder1)
7+
{
8+
// Arrange
9+
DynamicProgramming dp;
10+
int n = 4;
11+
int expectedNumberOfWays = 3;
12+
13+
// Act
14+
int actualNumberOfWays = dp.RecursiveCountWaysToReachNthStairExcludeOrder(n);
15+
16+
// Assert
17+
ASSERT_EQ(expectedNumberOfWays, actualNumberOfWays);
18+
}
19+
20+
TEST(CountWaysToReachNthStairExcludeOrderTest, DpCountWaysToReachNthStairExcludeOrder1)
21+
{
22+
// Arrange
23+
DynamicProgramming dp;
24+
int n = 4;
25+
int expectedNumberOfWays = 3;
26+
27+
// Act
28+
int actualNumberOfWays = dp.DpCountWaysToReachNthStairExcludeOrder(n);
29+
30+
// Assert
31+
ASSERT_EQ(expectedNumberOfWays, actualNumberOfWays);
32+
}
33+
}

test/0005_DynamicProgramming/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ add_executable(
2525
0009_FriendsPairingProblemTest.cc
2626
0010_WaysToCoverDistanceTest.cc
2727
0011_CountWaysToReachNthStairIncludeOrderTest.cc
28+
0012_CountWaysToReachNthStairExcludeOrderTest.cc
2829

2930
)
3031

0 commit comments

Comments
 (0)