Home
DSA Patterns
Dynamic Programming
Domino and Tromino Tiling
Domino and Tromino Tiling (LeetCode 790)
Domino and Tromino Tiling (LeetCode 790): count the ways to tile a 2 × n board with dominoes and L-shaped trominoes. dp[n] = 2 · dp[n − 1] + dp[n − 3], starting from dp[0] = 1, dp[1] = 1 and dp[2] = 2, modulo 10⁹ + 7.
Problem Understanding
Domino and Tromino Tiling (LeetCode 790): you have two kinds of tile, a 2 × 1 domino and an L-shaped tromino covering three cells, and both may be rotated. Return the number of ways to tile a 2 × n board, modulo 10⁹ + 7. Two tilings differ if some pair of neighbouring cells is covered by one tile in one tiling and not in the other.
Examples: n = 3 → 5; n = 1 → 1.
The Intuition: Look at How the Board Ends
Count tilings by the last piece placed at the right edge.
- A full board of width n ends with an upright domino (leaving width n − 1), two flat dominoes (width n − 2), or a tromino that completes a board with one cell sticking out (two mirror images).
- Boards with one cell sticking out end with a flat domino or a tromino, which ties them back to full boards.
- Eliminating the jagged boards leaves one line:
dp[n] = 2 · dp[n − 1] + dp[n − 3].
With dp[0] = 1, dp[1] = 1 and dp[2] = 2, fill the table left to right, as in N-th Tribonacci Number, which also looks three steps back. Introduction to Dynamic Programming covers the table-filling idea.
Domino and Tromino Tiling Visualization
OUTLINED = dp[i − 1], COUNTED TWICE · TINTED = dp[i − 3]
BASE CASES
dp[i] = tilings of a 2 × i board
0
1
2
3
4
5
6
7
i
:
dp[i]
:
Answer
:
dp[0] = 1 (the empty board), dp[1] = 1 (one upright domino), dp[2] = 2 (two upright or two flat)
Next
dp[3] = 2 × dp[2] + dp[0] = 2 × 2 + 1 = 5
n
Each cell is dp[i], the number of tilings of a 2 × i board. As dp[i] is filled, dp[i − 1] is outlined (it counts twice) and dp[i − 3] is tinted. Try Example 1 (n = 3) and n = 12.
Domino and Tromino Tiling Dry Run
Input: n = 7.
| i | 2 × dp[i − 1] | + dp[i − 3] | dp[i] |
|---|---|---|---|
| 0 | 1 | ||
| 1 | 1 | ||
| 2 | 2 | ||
| 3 | 2 × 2 | + 1 | 5 |
| 4 | 2 × 5 | + 1 | 11 |
| 5 | 2 × 11 | + 2 | 24 |
| 6 | 2 × 24 | + 5 | 53 |
| 7 | 2 × 53 | + 11 | 117 |
Answer: 117.
Domino and Tromino Tiling LeetCode Solution
Three base cases and one loop. The table is sized at least 3 so that n = 1 can still set dp[2].
Domino and Tromino Tiling Code
1var numTilings = function(n) {2 const MOD = 1_000_000_007;3 const dp = new Array(Math.max(n, 2) + 1).fill(0);4 dp[0] = 1;5 dp[1] = 1;6 dp[2] = 2;7 for (let i = 3; i <= n; i++) {8 dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD;9 }10 return dp[n];11};
Domino and Tromino Tiling in Python, Java, C++, TypeScript and C
Domino and Tromino Tiling in Python
class Solution:
def numTilings(self, n: int) -> int:
MOD = 10**9 + 7
dp = [0] * (max(n, 2) + 1)
dp[0], dp[1], dp[2] = 1, 1, 2
for i in range(3, n + 1):
dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD
return dp[n]Domino and Tromino Tiling in Java
class Solution {
public int numTilings(int n) {
final int MOD = 1_000_000_007;
long[] dp = new long[Math.max(n, 2) + 1];
dp[0] = 1;
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD;
}
return (int) dp[n];
}
}Domino and Tromino Tiling in C++
class Solution {
public:
int numTilings(int n) {
const long long MOD = 1'000'000'007;
vector<long long> dp(max(n, 2) + 1);
dp[0] = 1;
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD;
}
return dp[n];
}
};Domino and Tromino Tiling in TypeScript
function numTilings(n: number): number {
const MOD = 1_000_000_007;
const dp: number[] = new Array(Math.max(n, 2) + 1).fill(0);
dp[0] = 1;
dp[1] = 1;
dp[2] = 2;
for (let i = 3; i <= n; i++) {
dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD;
}
return dp[n];
}Domino and Tromino Tiling in C
int numTilings(int n) {
const long long MOD = 1000000007;
long long dp[1001] = {0};
dp[0] = 1;
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD;
}
return (int)dp[n];
}Edge Cases & Common Mistakes
- Using the domino-only recurrence:
dp[n] = dp[n − 1] + dp[n − 2](Fibonacci) ignores trominoes and gives 3 for n = 3 instead of 5. - dp[0] = 0: the recurrence then gives 4 at n = 3.
- Forgetting the modulo: values overflow 32-bit integers from n = 29 on.
- An array of size n + 1 when n = 1: setting dp[2] writes past the end; size it for at least 3 entries.
Summary
dp[0] = 1, dp[1] = 1, dp[2] = 2, and dp[i] = 2 · dp[i − 1] + dp[i − 3] modulo 10⁹ + 7. O(n) time, O(n) space (O(1) with three variables).
More practice: DSA interview questions with answers · patterns cheat sheet
