-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathDomino and Tromino Tiling.cpp
40 lines (26 loc) · 1.07 KB
/
Domino and Tromino Tiling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Solution by Rahul Surana
***********************************************************
You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return the number of ways to tile an 2 x n board. Since the answer may be very large, return it modulo 109 + 7.
In a tiling, every square must be covered by a tile. Two tilings are different if and only if
there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
vector<long> dp;
int MOD = 1000000007;
long df(int n){
if(dp[n] != -1) return dp[n];
else if(n == 1) return 1;
else if(n == 2) return 2;
else if(n == 3) return 5;
return dp[n] = (((2*df(n-1))%MOD)+df(n-3)%MOD )%MOD;
}
int numTilings(int n) {
dp.resize(n+1,-1);
return df(n);
}
};