Skip to content

Commit 679e272

Browse files
committed
feature: ways to cover sol added
1 parent 922a1b6 commit 679e272

File tree

5 files changed

+219
-0
lines changed

5 files changed

+219
-0
lines changed
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: 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+
}

source/0005_DynamicProgramming/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ set(0005DYNAMICPROGRAMMING_SOURCES
99
0007_DecodeWays.cc
1010
0008_TilingProblem.cc
1111
0009_FriendsPairingProblem.cc
12+
0010_WaysToCoverDistance.cc
13+
1214
)
1315

1416
# Create a library target
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include<gtest/gtest.h>
2+
#include "../../include/0005_DynamicProgramming/0010_WaysToCoverDistance.h"
3+
4+
namespace WaysToCoverDistance
5+
{
6+
TEST(WaysToCoverDistanceTest, RecursiveWaysToCoverDistance1)
7+
{
8+
// Arrange
9+
DynamicProgramming dp;
10+
int distance = 3;
11+
int expectedNumberOfWaysToCoverDistance = 4;
12+
13+
// Act
14+
int actualNumberOfWaysToCoverDistance = dp.RecursiveWaysToCoverDistance(distance);
15+
16+
// Assert
17+
ASSERT_EQ(expectedNumberOfWaysToCoverDistance, actualNumberOfWaysToCoverDistance);
18+
}
19+
20+
TEST(WaysToCoverDistanceTest, RecursiveWaysToCoverDistance2)
21+
{
22+
// Arrange
23+
DynamicProgramming dp;
24+
int distance = 4;
25+
int expectedNumberOfWaysToCoverDistance = 7;
26+
27+
// Act
28+
int actualNumberOfWaysToCoverDistance = dp.RecursiveWaysToCoverDistance(distance);
29+
30+
// Assert
31+
ASSERT_EQ(expectedNumberOfWaysToCoverDistance, actualNumberOfWaysToCoverDistance);
32+
}
33+
34+
TEST(WaysToCoverDistanceTest, RecursiveWaysToCoverDistanc3)
35+
{
36+
// Arrange
37+
DynamicProgramming dp;
38+
int distance = 5;
39+
int expectedNumberOfWaysToCoverDistance = 13;
40+
41+
// Act
42+
int actualNumberOfWaysToCoverDistance = dp.RecursiveWaysToCoverDistance(distance);
43+
44+
// Assert
45+
ASSERT_EQ(expectedNumberOfWaysToCoverDistance, actualNumberOfWaysToCoverDistance);
46+
}
47+
48+
TEST(WaysToCoverDistanceTest, RecursiveWaysToCoverDistanc4)
49+
{
50+
// Arrange
51+
DynamicProgramming dp;
52+
int distance = 6;
53+
int expectedNumberOfWaysToCoverDistance = 24;
54+
55+
// Act
56+
int actualNumberOfWaysToCoverDistance = dp.RecursiveWaysToCoverDistance(distance);
57+
58+
// Assert
59+
ASSERT_EQ(expectedNumberOfWaysToCoverDistance, actualNumberOfWaysToCoverDistance);
60+
}
61+
62+
TEST(WaysToCoverDistanceTest, DpWaysToCoverDistance1)
63+
{
64+
// Arrange
65+
DynamicProgramming dp;
66+
int distance = 3;
67+
int expectedNumberOfWaysToCoverDistance = 4;
68+
69+
// Act
70+
int actualNumberOfWaysToCoverDistance = dp.DpWaysToCoverDistance(distance);
71+
72+
// Assert
73+
ASSERT_EQ(expectedNumberOfWaysToCoverDistance, actualNumberOfWaysToCoverDistance);
74+
}
75+
76+
TEST(WaysToCoverDistanceTest, DpWaysToCoverDistance2)
77+
{
78+
// Arrange
79+
DynamicProgramming dp;
80+
int distance = 4;
81+
int expectedNumberOfWaysToCoverDistance = 7;
82+
83+
// Act
84+
int actualNumberOfWaysToCoverDistance = dp.DpWaysToCoverDistance(distance);
85+
86+
// Assert
87+
ASSERT_EQ(expectedNumberOfWaysToCoverDistance, actualNumberOfWaysToCoverDistance);
88+
}
89+
90+
TEST(WaysToCoverDistanceTest, DpWaysToCoverDistance3)
91+
{
92+
// Arrange
93+
DynamicProgramming dp;
94+
int distance = 5;
95+
int expectedNumberOfWaysToCoverDistance = 13;
96+
97+
// Act
98+
int actualNumberOfWaysToCoverDistance = dp.DpWaysToCoverDistance(distance);
99+
100+
// Assert
101+
ASSERT_EQ(expectedNumberOfWaysToCoverDistance, actualNumberOfWaysToCoverDistance);
102+
}
103+
104+
TEST(WaysToCoverDistanceTest, DpWaysToCoverDistance4)
105+
{
106+
// Arrange
107+
DynamicProgramming dp;
108+
int distance = 6;
109+
int expectedNumberOfWaysToCoverDistance = 24;
110+
111+
// Act
112+
int actualNumberOfWaysToCoverDistance = dp.DpWaysToCoverDistance(distance);
113+
114+
// Assert
115+
ASSERT_EQ(expectedNumberOfWaysToCoverDistance, actualNumberOfWaysToCoverDistance);
116+
}
117+
}

test/0005_DynamicProgramming/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_executable(
2323
0007_DecodeWaysTest.cc
2424
0008_TilingProblemTest.cc
2525
0009_FriendsPairingProblemTest.cc
26+
0010_WaysToCoverDistanceTest.cc
2627

2728
)
2829

0 commit comments

Comments
 (0)