Everyday has one random leetcode quiz. Today’s quiz is 790. Domino and Tromino Tiling
Description:
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.
Get Jimmy’s stories in your inbox
Join Medium for free to get updates from this writer.
Example 1:
Input: n = 3
Output: 5
Explanation: The five different ways are show above.Example 2:
Input: n = 1
Output: 1n = 1, 1 case
|
|n = 2, 1 case
||
||n = 3, 5 cases
||| --| |-- ┌ - | | - ┐
|||, --|,|--, | - ┘, └ - |n = 4, 11 cases
||| --| |-- ┌ - | | - ┐ |
|||, --|,|--, | - ┘, └ - | add | at left and right, get 9 cases
┌ - - ┐ | - - |
| - - |,└ - - ┘, get 2 cases…
At the end, we return ans as answer.
The time complexity is O(n)
/**
* @param {number} n
* @return {number}
*/
var numTilings = function (n) {
const MOD = 1e9 + 7
const dp = Array(n + 1).fill(0)
dp[0] = 1
dp[1] = 1
dp[2] = 2
dp[3] = 5
for (let i = 4; i <= n; i++) {
dp[i] = (2 * dp[i - 1] % MOD + dp[i - 3]) % MOD
}
return dp[n]
};





![Python DSA Coding Interview Series:11/29 [Linked Lists]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*RpzhG5-m6tzjX-E1XdLsWA.png)