\\Given a positive integer N, the task is to find the number of ways to fill the board of dimension 2*N with a tile of dimensions 2 Ć 1, 1 Ć 2, (also known as domino) and an āLā shaped tile(also know as tromino) show below that can be rotated by 90 degrees.
The L shape tile: XX X After rotating L shape tile by 90: XX X or X XX
Examples:
Input: N = 3
Output: 5
Explanation:
Below is the image to illustrate all the combinations:Input: N = 1
Output: 1
Approach: The given problem can be solved based on the following observations by:
Letās define a 2 state, dynamic programming say dp[i, j] denoting one of the following arrangements in column index i.
- The current column can be filled with 1, 2 Ć 1 dominos in state 0, if the previous column had state 0.
- The current column can be filled with 2, 1 Ć 2 dominos horizontally in state 0, if the i ā 2 column has state 0.
- The current column can be filled with an āLā shaped domino in state 1 and state 2, if the previous column had state 0.
- The current column can be filled with 1 Ć 2 shaped domino in state 1 if the previous column has state 2 or in state 2 if the previous column has state 1.
- Therefore, the transition of the state can be defined as the following:
- dp[i][0] = (dp[i ā 1][0] + dp[i ā 2][0]+ dp[i ā 2][1] + dp[i ā 2][2]).
- dp[i][1] = dp[i ā 1][0] + dp[i ā 1][2].
- dp[i][2] = dp[i ā 1][0] + dp[i ā 1][1].
Based on the above observations, follow the steps below to solve the problem:
- If the value of N is less than 3, then print N as the total number of ways.
- Initialize a 2-dimensional array, say dp[][3] that stores all the states of the dp.
- Consider the Base Case: dp[0][0] = dp[1][0] = dp[1][1] = dp[1][2] = 1.
- Iterate over the given range [2, N] and using the variable i and perform the following transitions in the dp as:
- dp[i][0] equals (dp[i ā 1][0] + dp[i ā 2][0]+ dp[i ā 2][1] + dp[i ā 2][2]).
- dp[i][1] equals dp[i ā 1][0] + dp[i ā 1][2].
- dp[i][2] equals dp[i ā 1][0] + dp[i ā 1][1].
- After completing the above steps, print the total number of ways stored in dp[N][0].
Below is the implementation of the above approach:
C++
// C++ program for the above approach Ā
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; Ā
// Function to find the total number // of ways to tile a 2*N board using // the given types of tile int numTilings( int N) { Ā Ā Ā Ā // If N is less than 3 Ā Ā Ā Ā if (N < 3) { Ā Ā Ā Ā Ā Ā Ā Ā return N; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Store all dp-states Ā Ā Ā Ā vector<vector< long long > > dp( Ā Ā Ā Ā Ā Ā Ā Ā N + 1, vector< long long >(3, 0)); Ā
Ā Ā Ā Ā // Base Case Ā Ā Ā Ā dp[0][0] = dp[1][0] = 1; Ā Ā Ā Ā dp[1][1] = dp[1][2] = 1; Ā
Ā Ā Ā Ā // Traverse the range [2, N] Ā Ā Ā Ā for ( int i = 2; i <= N; i++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][0] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][0] = (dp[i - 1][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2][1] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2][2]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][1] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][1] = (dp[i - 1][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 1][2]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][2] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][2] = (dp[i - 1][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 1][1]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Return the number of ways as Ā Ā Ā Ā // the value of dp[N][0] Ā Ā Ā Ā return dp[N][0]; } Ā
// Driver Code int main() { Ā Ā Ā Ā int N = 3; Ā Ā Ā Ā cout << numTilings(N); Ā
Ā Ā Ā Ā return 0; } |
Java
// Java program for the above approach import java.util.Arrays; Ā
class GFG{ Ā
public static long MOD = 1000000007l; Ā
// Function to find the total number // of ways to tile a 2*N board using // the given types of tile public static long numTilings( int N) { Ā Ā Ā Ā Ā Ā Ā Ā Ā // If N is less than 3 Ā Ā Ā Ā if (N < 3 ) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return N; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Store all dp-states Ā Ā Ā Ā long [][] dp = new long [N + 1 ][ 3 ]; Ā
Ā Ā Ā Ā for ( long [] row : dp) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Arrays.fill(row, 0 ); Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā // Base Case Ā Ā Ā Ā dp[ 0 ][ 0 ] = dp[ 1 ][ 0 ] = 1 ; Ā Ā Ā Ā dp[ 1 ][ 1 ] = dp[ 1 ][ 2 ] = 1 ; Ā
Ā Ā Ā Ā // Traverse the range [2, N] Ā Ā Ā Ā for ( int i = 2 ; i <= N; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][0] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][ 0 ] = (dp[i - 1 ][ 0 ] + dp[i - 2 ][ 0 ] + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i - 2 ][ 1 ] + dp[i - 2 ][ 2 ]) % MOD; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][1] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][ 1 ] = (dp[i - 1 ][ 0 ] + dp[i - 1 ][ 2 ]) % MOD; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][2] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][ 2 ] = (dp[i - 1 ][ 0 ] + dp[i - 1 ][ 1 ]) % MOD; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Return the number of ways as Ā Ā Ā Ā // the value of dp[N][0] Ā Ā Ā Ā return dp[N][ 0 ]; } Ā
// Driver Code public static void main(String args[]) { Ā Ā Ā Ā int N = 3 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(numTilings(N)); } } Ā
// This code is contributed by gfgking |
Python3
# Python3 program for the above approache9 + 7; Ā
# Function to find the total number # of ways to tile a 2*N board using # the given types of tile MOD = 1e9 + 7 Ā
def numTilings(N): Ā Ā Ā Ā Ā Ā Ā Ā Ā # If N is less than 3 Ā Ā Ā Ā if (N < 3 ): Ā Ā Ā Ā Ā Ā Ā Ā return N Ā
Ā Ā Ā Ā # Store all dp-states Ā Ā Ā Ā dp = [[ 0 ] * 3 for i in range (N + 1 )] Ā
Ā Ā Ā Ā # Base Case Ā Ā Ā Ā dp[ 0 ][ 0 ] = dp[ 1 ][ 0 ] = 1 Ā Ā Ā Ā dp[ 1 ][ 1 ] = dp[ 1 ][ 2 ] = 1 Ā
Ā Ā Ā Ā # Traverse the range [2, N] Ā Ā Ā Ā for i in range ( 2 , N + 1 ): Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Update the value of dp[i][0] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][ 0 ] = (dp[i - 1 ][ 0 ] + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i - 2 ][ 0 ] + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i - 2 ][ 1 ] + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i - 2 ][ 2 ]) % MOD Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Update the value of dp[i][1] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][ 1 ] = (dp[i - 1 ][ 0 ] + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i - 1 ][ 2 ]) % MOD Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Update the value of dp[i][2] Ā Ā Ā Ā Ā Ā Ā Ā dp[i][ 2 ] = (dp[i - 1 ][ 0 ] + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i - 1 ][ 1 ]) % MOD Ā
Ā Ā Ā Ā # Return the number of ways as Ā Ā Ā Ā # the value of dp[N][0] Ā Ā Ā Ā return int (dp[N][ 0 ]) Ā
# Driver Code N = 3 Ā
print (numTilings(N)) Ā
# This code is contributed by gfgking |
C#
// C# program for the above approach using System; using System.Collections.Generic; Ā
class GFG{ Ā
static int MOD = 1000000007; Ā
// Function to find the total number // of ways to tile a 2*N board using // the given types of tile static int numTilings( int N) { Ā Ā Ā Ā // If N is less than 3 Ā Ā Ā Ā if (N < 3) { Ā Ā Ā Ā Ā Ā Ā Ā return N; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Store all dp-states Ā Ā Ā Ā int [,]dp = new int [N+1,3]; Ā
Ā Ā Ā Ā // Base Case Ā Ā Ā Ā dp[0,0] = dp[1,0] = 1; Ā Ā Ā Ā dp[1,1] = dp[1,2] = 1; Ā
Ā Ā Ā Ā // Traverse the range [2, N] Ā Ā Ā Ā for ( int i = 2; i <= N; i++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i,0] Ā Ā Ā Ā Ā Ā Ā Ā dp[i,0] = (dp[i - 1,0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2,0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2,1] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2,2]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i,1] Ā Ā Ā Ā Ā Ā Ā Ā dp[i,1] = (dp[i - 1,0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 1,2]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i,2] Ā Ā Ā Ā Ā Ā Ā Ā dp[i,2] = (dp[i - 1,0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 1,1]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Return the number of ways as Ā Ā Ā Ā // the value of dp[N,0] Ā Ā Ā Ā return dp[N,0]; } Ā
// Driver Code public static void Main() { Ā Ā Ā Ā int N = 3; Ā Ā Ā Ā Console.Write(numTilings(N)); } } Ā
// This code is contributed by SURENDRA_GANGWAR. |
Javascript
<script> Ā
Ā Ā Ā Ā Ā Ā Ā Ā // JavaScript program for the above approache9 + 7; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Function to find the total number Ā Ā Ā Ā Ā Ā Ā Ā // of ways to tile a 2*N board using Ā Ā Ā Ā Ā Ā Ā Ā // the given types of tile Ā Ā Ā Ā Ā Ā Ā Ā const MOD = 1e9 + 7; Ā Ā Ā Ā Ā Ā Ā Ā function numTilings(N) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If N is less than 3 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (N < 3) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return N; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Store all dp-states Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let dp = Array(N + 1).fill().map(() => Array(3).fill(0)) Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Base Case Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[0][0] = dp[1][0] = 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[1][1] = dp[1][2] = 1; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the range [2, N] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 2; i <= N; i++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][0] = (dp[i - 1][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2][1] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 2][2]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][1] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][1] = (dp[i - 1][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 1][2]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the value of dp[i][2] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i][2] = (dp[i - 1][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp[i - 1][1]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā % MOD; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Return the number of ways as Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the value of dp[N][0] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[N][0]; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Driver Code Ā
Ā Ā Ā Ā Ā Ā Ā Ā let N = 3; Ā Ā Ā Ā Ā Ā Ā Ā document.write(numTilings(N)); Ā
Ā
Ā
// This code is contributed by Potta Lokesh Ā Ā Ā Ā </script> |
5
Time Complexity: O(N)
Auxiliary Space: O(N)
Easy Dp Algorithm Using Extra O(n) space.
C++
#include<bits/stdc++.h> #include<iostream> #define ll long long ll mod=1e9+7; using namespace std; Ā
void the_solver( int n){ Ā Ā Ā Ā Ā Ā vector<ll>dp(n+1,0); Ā Ā Ā Ā Ā Ā dp[0]=1; Ā Ā Ā Ā Ā Ā dp[1]=1;dp[2]=2;dp[3]=5; Ā Ā Ā Ā Ā Ā if (n<=3){ Ā Ā Ā Ā Ā Ā Ā Ā cout<<dp[n]<<endl; Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā for ( int i=4;i<=n;i++){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i]=2*(dp[i-1])+dp[i-3]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i]%=mod; Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā cout<<dp[n]<<endl; Ā Ā Ā Ā Ā Ā return ; } Ā
signed main(){ Ā Ā Ā int n=3; Ā Ā Ā the_solver(n); Ā Ā Ā Ā return 0; } |
Java
//Java code for the above approach import java.util.*; Ā
class Main { Ā Ā Ā Ā static final long mod = 1000000000L + 7 ; Ā
Ā Ā Ā Ā // Function to solve problem Ā Ā Ā Ā static void theSolver( int n) { Ā Ā Ā Ā Ā Ā Ā Ā // Create an array of size n+1 to store the dp values Ā Ā Ā Ā Ā Ā Ā Ā long [] dp = new long [n + 1 ]; Ā Ā Ā Ā Ā Ā Ā Ā dp[ 0 ] = 1 ; Ā Ā Ā Ā Ā Ā Ā Ā dp[ 1 ] = 1 ; Ā Ā Ā Ā Ā Ā Ā Ā dp[ 2 ] = 2 ; Ā Ā Ā Ā Ā Ā Ā Ā dp[ 3 ] = 5 ; Ā Ā Ā Ā Ā Ā Ā Ā // If n is less than or equal to 3 Ā Ā Ā Ā Ā Ā Ā Ā if (n <= 3 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(dp[n]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā // Iterate through the array Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 4 ; i <= n; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i] = 2 * (dp[i - 1 ]) + dp[i - 3 ]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i] %= mod; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(dp[n]); Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā public static void main(String[] args) { Ā Ā Ā Ā Ā Ā Ā Ā int n = 3 ; Ā Ā Ā Ā Ā Ā Ā Ā theSolver(n); Ā Ā Ā Ā } } |
Python3
mod = int ( 1e9 + 7 ) Ā
def the_solver(n): Ā Ā Ā Ā dp = [ 0 ] * (n + 1 ) Ā Ā Ā Ā dp[ 0 ] = 1 Ā Ā Ā Ā dp[ 1 ] = 1 Ā Ā Ā Ā dp[ 2 ] = 2 Ā Ā Ā Ā dp[ 3 ] = 5 Ā Ā Ā Ā if n < = 3 : Ā Ā Ā Ā Ā Ā Ā Ā print (dp[n]) Ā Ā Ā Ā Ā Ā Ā Ā return Ā Ā Ā Ā for i in range ( 4 , n + 1 ): Ā Ā Ā Ā Ā Ā Ā Ā dp[i] = 2 * dp[i - 1 ] + dp[i - 3 ] Ā Ā Ā Ā Ā Ā Ā Ā dp[i] % = mod Ā Ā Ā Ā print (dp[n]) Ā Ā Ā Ā return Ā
n = 3 the_solver(n) Ā
# This codez is contributed by lokeshpotta20. |
Javascript
function the_solver(n) { Ā Ā Ā Ā let dp= new Array(n).fill(0); Ā Ā Ā Ā dp[0]=1; Ā Ā Ā Ā dp[1]=1; Ā Ā Ā Ā dp[2]=2; Ā Ā Ā Ā dp[3]=5; Ā Ā Ā Ā if (n<=3) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā document.write(dp[n]); Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā } Ā Ā Ā Ā for (let i=4;i<=n;i++){ Ā Ā Ā Ā Ā Ā Ā Ā dp[i]=2*(dp[i-1])+dp[i-3]; Ā Ā Ā Ā Ā Ā Ā Ā dp[i]%=mod; Ā Ā Ā Ā } Ā Ā Ā Ā document.write(dp[n]); Ā Ā Ā Ā return ; } Ā
Ā Ā Ā let n=3; Ā Ā Ā the_solver(n); |
C#
using System; using System.Linq; using System.Collections.Generic; Ā
class GFG { Ā Ā Ā Ā static int mod=1000000007; Ā
Ā Ā Ā Ā static void the_solver( int n) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int [] dp= new int [n+1]; Ā Ā Ā Ā Ā Ā Ā Ā for ( int i=0; i<n+1; i++) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i]=0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[0]=1; Ā Ā Ā Ā Ā Ā Ā Ā dp[1]=1; Ā Ā Ā Ā Ā Ā Ā Ā dp[2]=2; Ā Ā Ā Ā Ā Ā Ā Ā dp[3]=5; Ā Ā Ā Ā Ā Ā Ā Ā if (n<=3){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(dp[n]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā for ( int i=4;i<=n;i++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i]=2*(dp[i-1])+dp[i-3]; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i]%=mod; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(dp[n]); Ā Ā Ā Ā Ā Ā Ā Ā return ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā static public void Main() Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā int n=3; Ā Ā Ā Ā Ā Ā Ā Ā the_solver(n); Ā Ā Ā Ā }Ā Ā Ā } |
5
Time Complexity: O(N).
Auxiliary Space: O(N).
Recursion Algorithm:
Types of domino and tromino
When no spaces
T1: Vertical domino
T2: Horizaontal domino
T3: Ā 2 different types of Tromino
When there is a space
T4: Horizontal domino
T5/T6: 2 different types of Tromino depending upon the space
These steps describe a recursive algorithm to count the number of ways to tile a 2 x n grid using the given set of tiles, with T1 through T6 representing the different types of tiles.Ā
No Previous Space Present:
- Place T1 and move to i+1: solve(i+1, previousGap=false)
- Place T2 in pair and move to i+2: solve(i+2, previousGap=false)
- Place either T3 or T4 (consider both cases) and move to i+2 with gap at i+1th column: 2*solve(i+2, previousGap=true)
Previous Space Present:
- Place T5 or T6 & fill previous gap (consider only 1 because depending on current configuration, only 1 grid out of them will fit) and move to i+1 with no previous gaps remaining: solve(i+1, previousGap=false)
- Place T2 & fill previous gap and move to i+1 with gap present in ith column: solve(i+1, previousGap=true)
C++
// C++ program for the above approach Ā
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; Ā
// Function to find the total number // of ways to tile a 2*N board using // the given types of tile Ā
int func( int idx, int space, int n) { Ā Ā Ā Ā if (idx > n + 1) { Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā if (idx == n + 1) { Ā Ā Ā Ā Ā Ā Ā Ā if (space == true ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā return 1; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Recursive case: try placing a tile horizontally or Ā Ā Ā Ā // vertically Ā Ā Ā Ā int cnt = 0; Ā Ā Ā Ā if (space == false ) { Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1, false , n); Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 2, false , n); Ā Ā Ā Ā Ā Ā Ā Ā cnt += 2 * func(idx + 2, true , n); Ā Ā Ā Ā } Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1, false , n); Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1, true , n); Ā Ā Ā Ā } Ā Ā Ā Ā return cnt % MOD; } Ā
// Wrapper function to call func with initial values int numTilings( int N) { return func(1, false , N); } Ā
// Driver Code int main() { Ā Ā Ā Ā int N = 3; Ā Ā Ā Ā cout << numTilings(N); Ā
Ā Ā Ā Ā return 0; } |
Java
import java.io.*; import java.util.*; Ā
// "static void main" must be defined in a public class. // java program for the above approach public class Main { Ā
Ā Ā static int MOD = 1000000007 ; Ā
Ā Ā // Function to find the total number Ā Ā // of ways to tile a 2*N board using Ā Ā // the given types of tile Ā
Ā Ā public static int func( int idx, boolean space, int n) Ā Ā { Ā Ā Ā Ā if (idx > n + 1 ) { Ā Ā Ā Ā Ā Ā return 0 ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā if (idx == n + 1 ) { Ā Ā Ā Ā Ā Ā if (space == true ) { Ā Ā Ā Ā Ā Ā Ā Ā return 0 ; Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā return 1 ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā int cnt = 0 ; Ā Ā Ā Ā if (space == false ) { Ā Ā Ā Ā Ā Ā cnt += func(idx + 1 , false , n); Ā Ā Ā Ā Ā Ā cnt += func(idx + 2 , false , n); Ā Ā Ā Ā Ā Ā cnt += 2 * func(idx + 2 , true , n); Ā Ā Ā Ā } Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā cnt += func(idx + 1 , false , n); Ā Ā Ā Ā Ā Ā cnt += func(idx + 1 , true , n); Ā Ā Ā Ā } Ā Ā Ā Ā return cnt % MOD; Ā Ā } Ā
Ā
Ā Ā public static int numTilings( int N){ return func( 1 , false , N); } Ā Ā public static void main(String[] args) { Ā Ā Ā Ā int N = 3 ; Ā Ā Ā Ā System.out.println(numTilings(N)); Ā
Ā Ā } } Ā
// The code is contributed by Nidhi goel. |
Python3
# Python program for the above approach Ā
MOD = int ( 1e9 + 7 ) Ā
# Function to find the total number # of ways to tile a 2*N board using # the given types of tile def func(idx, space, n): Ā Ā Ā Ā if idx > n + 1 : Ā Ā Ā Ā Ā Ā Ā Ā return 0 Ā
Ā Ā Ā Ā if idx = = n + 1 : Ā Ā Ā Ā Ā Ā Ā Ā if space = = True : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0 Ā Ā Ā Ā Ā Ā Ā Ā return 1 Ā
Ā Ā Ā Ā cnt = 0 Ā Ā Ā Ā if space = = False : Ā Ā Ā Ā Ā Ā Ā Ā cnt + = func(idx + 1 , False , n) Ā Ā Ā Ā Ā Ā Ā Ā cnt + = func(idx + 2 , False , n) Ā Ā Ā Ā Ā Ā Ā Ā cnt + = 2 * func(idx + 2 , True , n) Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā cnt + = func(idx + 1 , False , n) Ā Ā Ā Ā Ā Ā Ā Ā cnt + = func(idx + 1 , True , n) Ā Ā Ā Ā return cnt % MOD Ā
def numTilings(N): Ā Ā Ā Ā return func( 1 , False , N) Ā
# Driver Code if __name__ = = "__main__" : Ā Ā Ā Ā N = 3 Ā Ā Ā Ā print (numTilings(N)) Ā
# This code is contributed by codebraxnzt |
Javascript
const MOD = 1e9 + 7; Ā
// Function to find the total number // of ways to tile a 2*N board using // the given types of tile function func(idx, space, n) { if (idx > n + 1) { return 0; } if (idx === n + 1) { Ā Ā Ā Ā if (space === true ) { Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā } Ā Ā Ā Ā return 1; } Ā
let cnt = 0; if (space === false ) { Ā Ā Ā Ā cnt += func(idx + 1, false , n); Ā Ā Ā Ā cnt += func(idx + 2, false , n); Ā Ā Ā Ā cnt += 2 * func(idx + 2, true , n); } else { Ā Ā Ā Ā cnt += func(idx + 1, false , n); Ā Ā Ā Ā cnt += func(idx + 1, true , n); } return cnt % MOD; } Ā
function numTilings(N) { return func(1, false , N); } Ā
// Driver Code const N = 3; console.log(numTilings(N)); |
C#
using System; Ā
public class GFG { static int MOD = 1000000007; Ā Ā // Function to find the total number // of ways to tile a 2*N board using // the given types of tile public static int Func( int idx, bool space, int n) { Ā Ā Ā Ā if (idx > n + 1) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā if (idx == n + 1) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā if (space == true ) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā return 1; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā int cnt = 0; Ā Ā Ā Ā if (space == false ) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā cnt += Func(idx + 1, false , n); Ā Ā Ā Ā Ā Ā Ā Ā cnt += Func(idx + 2, false , n); Ā Ā Ā Ā Ā Ā Ā Ā cnt += 2 * Func(idx + 2, true , n); Ā Ā Ā Ā } Ā Ā Ā Ā else Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā cnt += Func(idx + 1, false , n); Ā Ā Ā Ā Ā Ā Ā Ā cnt += Func(idx + 1, true , n); Ā Ā Ā Ā } Ā Ā Ā Ā return cnt % MOD; } Ā
public static int NumTilings( int N) { Ā Ā Ā Ā return Func(1, false , N); } Ā
public static void Main( string [] args) { Ā Ā Ā Ā int N = 3; Ā Ā Ā Ā Console.WriteLine(NumTilings(N)); } } |
5
Time Complexity: O(3N) where N is the given number of columns of grid. We are branching out a max of 3 recursive calls each time and N such states giving total time complexity of O(3N)
Auxiliary Space: O(N), required for the recursive stack.
Memoization:
By analyzing the recursion tree from the previous approach, we can see that many redundant
Ā calls were made to the same function state. This is because the answer for a given set of parameters,Ā
i and space, will always be the same. To avoid repeating these calculations, we can use dynamicĀ
programming and store the result in a dp array. The dp[i][space] entry represents the number of
Ā ways to tile a grid starting from the ith column and whether the previous column had a spaceor not.Ā
If we find that dp[i][space] has already been calculated, we can simply return the stored result insteadĀ
of repeating the calculation.
We can cache the result of recursion to convert into a dp solution
C++
// C++ program for the above approach Ā
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; Ā
// Function to find the total number // of ways to tile a 2*N board using // the given types of tile Ā
long long int func( long long int idx, bool space, int n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vector<vector< int > >& dp) { Ā Ā Ā Ā if (idx > n + 1) { Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā if (idx == n + 1) { Ā Ā Ā Ā Ā Ā Ā Ā if (space == true ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā return 1; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā if (dp[idx][space] != -1) { Ā Ā Ā Ā Ā Ā Ā Ā return dp[idx][space]; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā long long int cnt = 0; Ā Ā Ā Ā if (space == false ) { Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1, false , n, dp); Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 2, false , n, dp); Ā Ā Ā Ā Ā Ā Ā Ā cnt += 2 * func(idx + 2, true , n, dp); Ā Ā Ā Ā } Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1, false , n, dp); Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1, true , n, dp); Ā Ā Ā Ā } Ā Ā Ā Ā return dp[idx][space] = cnt % MOD; } Ā
int numTilings( int N) { Ā Ā Ā Ā vector<vector< int > > dp(N + 2, vector< int >(3, -1)); Ā Ā Ā Ā return func(1, false , N, dp); } Ā
// Driver Code int main() { Ā Ā Ā Ā int N = 3; Ā Ā Ā Ā cout << numTilings(N); Ā
Ā Ā Ā Ā return 0; } |
Java
/*package whatever //do not write package name here */ import java.util.*; Ā
public class GFG { Ā
Ā Ā Ā Ā static final long MOD = ( long ) 1e9 + 7 ; Ā
Ā Ā Ā Ā // Function to find the total number Ā Ā Ā Ā // of ways to tile a 2*N board using Ā Ā Ā Ā // the given types of tile Ā Ā Ā Ā static long func( int idx, boolean space, int n, List<List<Long>> dp) { Ā Ā Ā Ā Ā Ā Ā Ā if (idx > n + 1 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0 ; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (idx == n + 1 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (space == true ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 1 ; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā if (dp.get(idx).get(space ? 1 : 0 ) != - 1 ) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp.get(idx).get(space ? 1 : 0 ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā long cnt = 0 ; Ā Ā Ā Ā Ā Ā Ā Ā if (!space) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1 , false , n, dp); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 2 , false , n, dp); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt += 2 * func(idx + 2 , true , n, dp); Ā Ā Ā Ā Ā Ā Ā Ā } else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1 , false , n, dp); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt += func(idx + 1 , true , n, dp); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā dp.get(idx).set(space ? 1 : 0 , cnt % MOD); Ā Ā Ā Ā Ā Ā Ā Ā return dp.get(idx).get(space ? 1 : 0 ); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā static int numTilings( int N) { Ā Ā Ā Ā Ā Ā Ā Ā List<List<Long>> dp = new ArrayList<>(); Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i <= N + 1 ; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.add( new ArrayList<>(Arrays.asList(-1L, -1L))); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā return ( int ) func( 1 , false , N, dp); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā public static void main(String[] args) { Ā Ā Ā Ā Ā Ā Ā Ā int N = 3 ; Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(numTilings(N)); Ā Ā Ā Ā } } |
C#
using System; Ā
public class Program { Ā Ā const long MOD = 1000000007; Ā
Ā Ā // Function to find the total number Ā Ā // of ways to tile a 2*N board using Ā Ā // the given types of tile Ā Ā public static long Func( int idx, bool space, int n, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā long [][] dp) Ā Ā { Ā Ā Ā Ā if (idx > n + 1) { Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā if (idx == n + 1) { Ā Ā Ā Ā Ā Ā if (space == true ) { Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā return 1; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā if (dp[idx][space ? 1 : 0] != -1) { Ā Ā Ā Ā Ā Ā return dp[idx][space ? 1 : 0]; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā long cnt = 0; Ā Ā Ā Ā if (!space) { Ā Ā Ā Ā Ā Ā cnt += Func(idx + 1, false , n, dp); Ā Ā Ā Ā Ā Ā cnt += Func(idx + 2, false , n, dp); Ā Ā Ā Ā Ā Ā cnt += 2 * Func(idx + 2, true , n, dp); Ā Ā Ā Ā } Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā cnt += Func(idx + 1, false , n, dp); Ā Ā Ā Ā Ā Ā cnt += Func(idx + 1, true , n, dp); Ā Ā Ā Ā } Ā Ā Ā Ā return dp[idx][space ? 1 : 0] = cnt % MOD; Ā Ā } Ā
Ā Ā public static int NumTilings( int N) Ā Ā { Ā Ā Ā Ā long [][] dp = new long [N + 2][]; Ā Ā Ā Ā for ( int i = 0; i < dp.Length; i++) { Ā Ā Ā Ā Ā Ā dp[i] = new long [3]; Ā Ā Ā Ā Ā Ā for ( int j = 0; j < dp[i].Length; j++) { Ā Ā Ā Ā Ā Ā Ā Ā dp[i][j] = -1; Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā Ā Ā return ( int )Func(1, false , N, dp); Ā Ā } Ā
Ā Ā public static void Main() Ā Ā { Ā Ā Ā Ā int N = 3; Ā Ā Ā Ā Console.WriteLine(NumTilings(N)); Ā Ā } } Ā
// This code is contributed by user_dtewbxkn77n |
Python3
def numTilings(N): Ā Ā Ā Ā MOD = 10 * * 9 + 7 # the modulo value Ā
Ā Ā Ā Ā def func(idx, space, n, dp): Ā Ā Ā Ā Ā Ā Ā Ā # base case 1: when we exceed the column limit, there is no way to tile further Ā Ā Ā Ā Ā Ā Ā Ā if idx > n + 1 : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0 Ā
Ā Ā Ā Ā Ā Ā Ā Ā # base case 2: when we reach the end of the column, we have completed the tiling Ā Ā Ā Ā Ā Ā Ā Ā if idx = = n + 1 : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if space = = True : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0 # if there is a space left, the tiling is not valid Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 1 # if there is no space left, the tiling is valid and we return 1 Ā
Ā Ā Ā Ā Ā Ā Ā Ā # if we have already computed the result for this position and state, we return it Ā Ā Ā Ā Ā Ā Ā Ā if dp[idx][ 1 if space else 0 ] ! = - 1 : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return dp[idx][ 1 if space else 0 ] Ā
Ā Ā Ā Ā Ā Ā Ā Ā cnt = 0 # variable to count the number of valid tilings Ā Ā Ā Ā Ā Ā Ā Ā if not space: # if there is no space to fill Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt + = func(idx + 1 , False , n, dp) # case 1: place a vertical tile at the current position Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt + = func(idx + 2 , False , n, dp) # case 2: place two horizontal tiles at the current position Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt + = 2 * func(idx + 2 , True , n, dp) # case 3: place two vertical tiles at the current position, leaving a space in between Ā Ā Ā Ā Ā Ā Ā Ā else : # if there is a space to fill Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt + = func(idx + 1 , False , n, dp) # case 1: place a vertical tile at the current position to fill the space Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt + = func(idx + 1 , True , n, dp) # case 2: leave the space empty Ā
Ā Ā Ā Ā Ā Ā Ā Ā # memoize the result for this position and state Ā Ā Ā Ā Ā Ā Ā Ā dp[idx][ 1 if space else 0 ] = cnt % MOD Ā Ā Ā Ā Ā Ā Ā Ā return dp[idx][ 1 if space else 0 ] Ā
Ā Ā Ā Ā # initialize the memoization table Ā Ā Ā Ā dp = [[ - 1 , - 1 ] for _ in range (N + 2 )] Ā
Ā Ā Ā Ā # call the recursive function to find the number of valid tilings Ā Ā Ā Ā return func( 1 , False , N, dp) Ā
# Driver Code N = 3 print (numTilings(N)) |
5
Time Complexity : O(N) where N is the given number of columns of grid
Auxiliary Space : O(N), required for recursive stack and maintaining dp
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!