Skip to content

Commit d235ea6

Browse files
authored
Merge pull request #93 from Debashis08/feature-dp-implementation
feature: tiling problem sol added
2 parents 7c93cb5 + 41426d0 commit d235ea6

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 "2 x n" board and tiles of size "2 x 1", the task is to count the number of ways to tile the given board using the 2 x 1 tiles.
12+
A tile can either be placed horizontally i.e., as a 1 x 2 tile or vertically i.e., as 2 x 1 tile.
13+
*/
14+
15+
namespace TilingProblem
16+
{
17+
class DynamicProgramming
18+
{
19+
private:
20+
int NumberOfWaysRecursiveHelper(int n);
21+
public:
22+
int RecursiveNumberOfWays(int n);
23+
int DpNumberOfWays(int n);
24+
};
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "../../include/0005_DynamicProgramming/0008_TilingProblem.h"
2+
3+
namespace TilingProblem
4+
{
5+
int DynamicProgramming::NumberOfWaysRecursiveHelper(int n)
6+
{
7+
if (n < 0)
8+
{
9+
return 0;
10+
}
11+
12+
if (n == 0)
13+
{
14+
return 1;
15+
}
16+
17+
int result = 0;
18+
result += this->NumberOfWaysRecursiveHelper(n - 1);
19+
result += this->NumberOfWaysRecursiveHelper(n - 2);
20+
21+
return result;
22+
}
23+
24+
int DynamicProgramming::RecursiveNumberOfWays(int n)
25+
{
26+
return this->NumberOfWaysRecursiveHelper(n);
27+
}
28+
29+
int DynamicProgramming::DpNumberOfWays(int n)
30+
{
31+
vector<int> dp(n + 1, 0);
32+
dp[0] = 1;
33+
dp[1] = 1;
34+
35+
for (int i = 2; i <= n; i++)
36+
{
37+
dp[i] = dp[i - 1] + dp[i - 2];
38+
}
39+
40+
return dp[n];
41+
}
42+
}

source/0005_DynamicProgramming/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(0005DYNAMICPROGRAMMING_SOURCES
77
0005_HouseRobber1.cc
88
0006_HouseRobber2.cc
99
0007_DecodeWays.cc
10+
0008_TilingProblem.cc
1011

1112
)
1213

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/0008_TilingProblem.h"
3+
4+
namespace TilingProblem
5+
{
6+
TEST(TilingProblem, RecursionTest01)
7+
{
8+
// Arrange
9+
DynamicProgramming dp;
10+
int nummberOfRows = 4;
11+
int expectedNumberOfWays = 5;
12+
13+
// Act
14+
int actualNumberOfWays = dp.RecursiveNumberOfWays(nummberOfRows);
15+
16+
// Assert
17+
ASSERT_EQ(expectedNumberOfWays, actualNumberOfWays);
18+
}
19+
20+
TEST(TilingProblem, DpTest01)
21+
{
22+
// Arrange
23+
DynamicProgramming dp;
24+
int nummberOfRows = 4;
25+
int expectedNumberOfWays = 5;
26+
27+
// Act
28+
int actualNumberOfWays = dp.DpNumberOfWays(nummberOfRows);
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
@@ -21,6 +21,7 @@ add_executable(
2121
0005_HouseRobber1Test.cc
2222
0006_HouseRobber2Test.cc
2323
0007_DecodeWaysTest.cc
24+
0008_TilingProblemTest.cc
2425

2526
)
2627

0 commit comments

Comments
 (0)