Last Updated: 25 Jul, 2020

Tiling Problem

Hard
Asked in companies
OptumOlaAdobe

Problem statement

You have been given a board where there are '2' rows and 'N' columns. You have an infinite supply of 2x1 tiles, and you can place a tile in the following ways:

1. Horizontally as 1x2 tile
2. Vertically as 2x1 tile

Count the number of ways to tile the given board using the available tiles.

Note :
The number of ways might be large so output your answer modulo 10^9 + 7.

Here an example of tile and board for 'N' = 4 :

Tiling Example

Input format :
The first and only line of each test case contains an Integer 'N' which denotes the size of the board, i.e. '2' rows and 'N' columns.
Output format :
For each test case, print the number of ways to tile the board modulo 10^9 + 7.
Note:
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
Constraints :
1 <= N <= 10^18

Where 'N' is the number of columns in the board. 

Time limit: 1 sec

Approaches

01 Approach

Try to place the tile to fill the unit column and calculate the number of ways from smaller sub-problems. Then use memoization to convert O(2^N) solution to an O(N) solution.

  1. At any point we are at ‘idx’ column then we can place our tile in two ways to fill this column.
    1. Option 1 -  1 Horizontal Tile

We can place in this way where we have ‘idx-1’ column filled.

          2.   Option 2 - 2 Vertical Tiles

We can place in this way where we have ‘idx-2’ column filled.   

     2. So, numberOfWays(n) = numberOfWays(n-1) + numberOfWays(n-2)

     3. Base cases are:

  1. When n = 1 there is only 1 way - Placing 1 Vertical Tile
  2. When n = 2 there are two ways - Placing 2 Vertical Tile and Placing 2 Horizontal Tiles.
  3. Also, take care of overflow using modulo 10^9 + 7.
  4. Lastly, use a DP Array of size N for memoization to save time over repetitive calls.

02 Approach

Try to place the tile to fill the unit column and calculate the number of ways from smaller sub-problems. We can use bottom-up DP with keeping the previous two values.

  1. At any point we are at ‘idx’ column then we can place our tile in two ways to fill this column.
    1. Option 1 -  1 Horizontal Tile

We can place in this way where we have ‘idx-1’ column filled.

          2. Option 2 - 2 Vertical Tiles

We can place in this way where we have ‘idx-2’ column filled.

      2. So, numberOfWaysCur = numberOfWaysPre1 + numberOfWaysPre2

  1. Where numberOfWaysCur is the number of ways to tile till 'current' column in the board.
  2. Where numberOfWaysPre1 is the number of ways to tile till 'current-1' column in the board.
  3. Where numberOfWaysPre2 is the number of ways to tile till 'current-2' column in the board.
  4. Base cases are:When n = 1 there is only one way - Placing 1 Vertical Tile

numberOfWaysPre2 = 1

         When n = 2 there are two ways - Placing 2 Vertical Tile and Placing 2 Horizontal Tiles

numberOfWaysPre1 = 2

     5.Now Iterate from i=3 to N and keep updating the three variables.

     6.Also, take care of overflow using modulo 10^9 + 7.

03 Approach

We can observe that the solution of this problem for 2xN Board is (N-1)th Fibonacci Number.

We can calculate the ‘N’th Fibonacci Number using Binary Matrix Exponentiation explained below. Here, Fn represents the n'th Fibonacci number. We will raise the matrix to the power of (n - 1) so that the top-left element gives the value of F(n + 1 - 1) = F(n).

 

[[1 1] (pow n)          [[(F(n+1)  F(n)]
 [1 0]]            =     [F(n)      F(n-1)]]

 

  1. So we need to find the (N - 1)th Power of this Matrix.
  2. We can write a function for the multiplication of two matrices.
  3. Then we can use binary exponentiation to calculate the ‘N’ power of this Matrix in log(N).
  4. Binary Exponentiation / Fast Exponentiation:

long long binpow(Matrix a, long long b) {

    if (b == 0)

        return 1;

    Matrix res = binpow(a, b / 2);

    if (b % 2)

        return res * res * a;

    else

        return res * res;

}

  1. Similarly, we can write a version for Matrix Exponentiation.
  2. Take care of overflow using modulo by 10^9 + 7 at each operation of Matrix Multiplication.