Count ways to tile an N – length board using tiles of specified dimensions
Last Updated :
19 Apr, 2023
Given an integer N, the task is to tile a board of dimensions N * 1 the task is to count the number of ways to tile a board using tiles of dimensions 1 * 1 and 2 * 1.
Examples:
Input: N = 2
Output: 5
Explanation:
Tile the board of size 2 x 1 by placing 1 x 1 tile in a Horizontal way and another 1 x 1 tile in a vertical way.
Tile the board of size 2 x 1 by placing 1 x 1 tile in a vertical way and another 1 x 1 tile in a Horizontal way.
Tile the board of size 2 x 1 by placing 1 x 1 tile in a Horizontal way and another 1 x 1 tile in a Horizontal way.
Tile the board of size 2 x 1 by placing 1 x 1 tile in a vertical way and another 1 x 1 tile in a vertical way.
Tile the board of size 2 x 1 by placing 2 x 1 tile in a horizontal way.
Therefore, the required output is 5.
Input: N = 1
Output: 2
Approach: The problem can be solved using Dynamic Programming. The idea is to divide the N x 1 board into smaller boards, then count the ways to tile the smaller boards. Finally, print the total count of ways to tile the N x 1 board. Follow the steps below to solve the problem:
- Following are the three ways to place the first tile:
- Place the 1 x 1 tile vertically.
- Place the 1 x 1 tile horizontally.
- Place the 2 x 1 tile horizontally.
- Therefore, the recurrence relation to solve the problem is as follows:
cntWays(N) = 2 * cntWays(N – 1) + cntWays(N – 2)
- Initialize an array say, dp[], where dp[i] stores the count of ways to tile the i x 1 board.
- Iterate over the range [2, N] and fill the array, dp[] using the tabulation method.
- Finally, print the value of dp[N].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long countWaysToTileBoard( long N)
{
long dp[N + 1];
dp[0] = 1;
dp[1] = 2;
for ( int i = 2; i <= N; i++) {
dp[i] = (2 * dp[i - 1]
+ dp[i - 2] );
}
cout << dp[N];
}
int main()
{
long N = 2;
countWaysToTileBoard(N);
return 0;
}
|
Java
class GFG{
static void countWaysToTileBoard( int N)
{
int dp[] = new int [N + 1 ];
dp[ 0 ] = 1 ;
dp[ 1 ] = 2 ;
for ( int i = 2 ; i <= N; i++)
{
dp[i] = ( 2 * dp[i - 1 ] +
dp[i - 2 ]);
}
System.out.print(dp[N]);
}
public static void main (String[] args)
{
int N = 2 ;
countWaysToTileBoard(N);
}
}
|
Python3
def countWaysToTileBoard( N) :
dp = [ 0 ] * (N + 1 )
dp[ 0 ] = 1
dp[ 1 ] = 2
for i in range ( 2 , N + 1 ) :
dp[i] = ( 2 * dp[i - 1 ] + dp[i - 2 ] )
print (dp[N])
if __name__ = = "__main__" :
N = 2
countWaysToTileBoard(N)
|
C#
using System;
class GFG{
static void countWaysToTileBoard( int N)
{
int []dp = new int [N + 1];
dp[0] = 1;
dp[1] = 2;
for ( int i = 2; i <= N; i++)
{
dp[i] = (2 * dp[i - 1] +
dp[i - 2]);
}
Console.Write(dp[N]);
}
public static void Main(String[] args)
{
int N = 2;
countWaysToTileBoard(N);
}
}
|
Javascript
<script>
function countWaysToTileBoard(N)
{
let dp = [];
dp[0] = 1;
dp[1] = 2;
for (let i = 2; i <= N; i++)
{
dp[i] = (2 * dp[i - 1] +
dp[i - 2]);
}
document.write(dp[N]);
}
let N = 2;
countWaysToTileBoard(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient approach: Space Optimization using variables
To optimize the space complexity of the previous code, we can use two variables to keep track of the last two elements of the DP array instead of keeping the entire DP array. This will reduce the space complexity from O(N) to O(1).
Steps that were to follow the above approach:
- The function initializes variables prev1 and prev2 to 1 and 2, respectively.
- The function then enters a loop that iterates over the range [2, N].
- For each i in this range, the function calculates the current element of the DP array using the recurrence relation curr = (2 * prev2 + prev1).
- It then updates the variables prev1 and prev2 to store the last two elements of the DP array for the next iteration.
- After the loop completes, the function returns the value of curr, which corresponds to the count of ways to tile a 1 x N board using 1 x 1 and 2 x 1 tiles.
Below is the code to implement above steps :
C++
#include <bits/stdc++.h>
using namespace std;
long countWaysToTileBoard( long N)
{
if (N == 0 || N == 1) {
return N + 1;
}
long prev1 = 1, prev2 = 2, curr;
for ( int i = 2; i <= N; i++) {
curr = (2 * prev2 + prev1);
prev1 = prev2;
prev2 = curr;
}
return curr;
}
int main()
{
long N = 2;
cout << countWaysToTileBoard(N);
return 0;
}
|
Java
import java.util.*;
public class TilingWays {
public static long countWaysToTileBoard( long N)
{
if (N == 0 || N == 1 ) {
return N + 1 ;
}
long prev1 = 1 , prev2 = 2 , curr = 0 ;
for ( int i = 2 ; i <= N; i++)
{
curr = ( 2 * prev2 + prev1) % (( long ) Math.pow( 10 , 9 ) + 7 );
prev1 = prev2;
prev2 = curr;
}
return curr;
}
public static void main(String[] args)
{
long N = 2 ;
System.out.println(countWaysToTileBoard(N));
}
}
|
Python
def countWaysToTileBoard(N):
if N = = 0 or N = = 1 :
return N + 1
prev1, prev2, curr = 1 , 2 , None
for i in range ( 2 , N + 1 ):
curr = 2 * prev2 + prev1
prev1 = prev2
prev2 = curr
return curr
if __name__ = = '__main__' :
N = 2
print (countWaysToTileBoard(N))
|
Javascript
function countWaysToTileBoard(N) {
if (N == 0 || N == 1) {
return N + 1;
}
let prev1 = 1, prev2 = 2, curr;
for (let i = 2; i <= N; i++) {
curr = (2 * prev2 + prev1);
prev1 = prev2;
prev2 = curr;
}
return curr;
}
function main() {
let N = 2;
console.log(countWaysToTileBoard(N));
}
main();
|
C#
using System;
public class GFG {
static long CountWaysToTileBoard( long N)
{
if (N == 0 || N == 1) {
return N + 1;
}
long prev1 = 1, prev2 = 2, curr = 0;
for ( int i = 2; i <= N; i++) {
curr = (2 * prev2 + prev1);
prev1 = prev2;
prev2 = curr;
}
return curr;
}
public static void Main()
{
long N = 2;
Console.WriteLine(CountWaysToTileBoard(N));
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...