Skip to content

Commit f05d37a

Browse files
committed
feature: count ways to reach nth stair include order sol added
1 parent 679e272 commit f05d37a

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
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+
#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+
}

source/0005_DynamicProgramming/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(0005DYNAMICPROGRAMMING_SOURCES
1010
0008_TilingProblem.cc
1111
0009_FriendsPairingProblem.cc
1212
0010_WaysToCoverDistance.cc
13+
0011_CountWaysToReachNthStairIncludeOrder.cc
1314

1415
)
1516

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/0011_CountWaysToReachNthStairIncludeOrder.h"
3+
4+
namespace CountWaysToReachNthStairIncludeOrder
5+
{
6+
TEST(CountWaysToReachNthStairIncludeOrderTest, RecursiveCountWaysToReachNthStairIncludeOrder1)
7+
{
8+
// Arrange
9+
DynamicProgramming dp;
10+
int n = 4;
11+
int expectedNumberOfWays = 5;
12+
13+
// Act
14+
int actualNumberOfWays = dp.RecursiveCountWaysToReachNthStairIncludeOrder(n);
15+
16+
// Assert
17+
ASSERT_EQ(expectedNumberOfWays, actualNumberOfWays);
18+
}
19+
20+
TEST(CountWaysToReachNthStairIncludeOrderTest, DpCountWaysToReachNthStairIncludeOrder1)
21+
{
22+
// Arrange
23+
DynamicProgramming dp;
24+
int n = 4;
25+
int expectedNumberOfWays = 5;
26+
27+
// Act
28+
int actualNumberOfWays = dp.DpCountWaysToReachNthStairIncludeOrder(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
@@ -24,6 +24,7 @@ add_executable(
2424
0008_TilingProblemTest.cc
2525
0009_FriendsPairingProblemTest.cc
2626
0010_WaysToCoverDistanceTest.cc
27+
0011_CountWaysToReachNthStairIncludeOrderTest.cc
2728

2829
)
2930

0 commit comments

Comments
 (0)