Domino and Tromino Tiling (LeetCode 790): Solution and Explanation
Count the ways to cover a 2 x n board with 2 x 1 dominoes and L-shaped trominoes, modulo 10^9 + 7.
Intuition
Tile the board from left to right. At any moment the right edge of what you have placed is either flat, or it has one cell sticking out into the next column. Those are the only two shapes worth tracking.
A flat edge at column i can be finished in three ways: a vertical domino on top of a flat edge at i - 1, two stacked horizontal dominoes on a flat edge at i - 2, or a tromino that fills the gap of a jagged edge at i - 1. The jagged edge can stick out of either row, which is why that term counts twice.
A jagged edge is finished either by a horizontal domino laid against a jagged edge one column back, or by a tromino placed on a flat edge two columns back.
Approach: Two-state DP (flat and jagged edge)
- If
n <= 2, returnn. - Let
full[i]count tilings of the firsticolumns andpart[i]count tilings of the firsticolumns plus one cell of columni + 1. - Set
full[0] = 1,full[1] = 1,full[2] = 2andpart[2] = 1. - For
ifrom 3 ton:full[i] = full[i-1] + full[i-2] + 2 * part[i-1]andpart[i] = part[i-1] + full[i-2], both mod10^9 + 7. - Return
full[n].
Step-by-step example
n = 3
- Bases: full = [1, 1, 2] for i = 0, 1, 2 and part[2] = 1 (a single tromino).
- full[3] = full[2] + full[1] + 2 * part[2] = 2 + 1 + 2 = 5.
- The five tilings are: three vertical dominoes, a vertical domino before or after two horizontal ones, and the two ways to interlock a pair of trominoes.
- part[3] = part[2] + full[1] = 1 + 1 = 2, which would be needed for n = 4.
- Return 5.
Code: Python, Java, C++
1class Solution:
2 def numTilings(self, n: int) -> int:
3 MOD = 10**9 + 7
4 if n <= 2:
5 return n
6 full = [0] * (n + 1)
7 part = [0] * (n + 1)
8 full[0], full[1], full[2] = 1, 1, 2
9 part[2] = 1
10 for i in range(3, n + 1):
11 full[i] = (full[i - 1] + full[i - 2] + 2 * part[i - 1]) % MOD
12 part[i] = (part[i - 1] + full[i - 2]) % MOD
13 return full[n]1class Solution {
2 public int numTilings(int n) {
3 final long MOD = 1000000007L;
4 if (n <= 2) {
5 return n;
6 }
7 long[] full = new long[n + 1];
8 long[] part = new long[n + 1];
9 full[0] = 1; full[1] = 1; full[2] = 2;
10 part[2] = 1;
11 for (int i = 3; i <= n; i++) {
12 full[i] = (full[i - 1] + full[i - 2] + 2 * part[i - 1]) % MOD;
13 part[i] = (part[i - 1] + full[i - 2]) % MOD;
14 }
15 return (int) full[n];
16 }
17}1class Solution {
2public:
3 int numTilings(int n) {
4 const long long MOD = 1000000007LL;
5 if (n <= 2) {
6 return n;
7 }
8 vector<long long> full(n + 1, 0);
9 vector<long long> part(n + 1, 0);
10 full[0] = 1; full[1] = 1; full[2] = 2;
11 part[2] = 1;
12 for (int i = 3; i <= n; i++) {
13 full[i] = (full[i - 1] + full[i - 2] + 2 * part[i - 1]) % MOD;
14 part[i] = (part[i - 1] + full[i - 2]) % MOD;
15 }
16 return (int) full[n];
17 }
18};Time and space complexity
O(n)
Each column is processed once with a constant number of additions.
O(n)
Two arrays of length n + 1. Only the last two entries of each are read, so a few variables are enough.
Other approaches
Single recurrence (time O(n), space O(1))
Substituting the jagged state away gives full[n] = 2 * full[n-1] + full[n-3]. It is shorter but hides why it works.
Matrix exponentiation (time O(log n), space O(1))
Write the recurrence as a 3 x 3 matrix and raise it to the n-th power. Useful only when n is huge.
Common mistakes
- Forgetting the factor 2 on part[i-1]: the jagged cell can stick out of the top row or the bottom row.
- Overflowing 32-bit integers before taking the modulus. Sums reach about 4 * 10^9, so use 64-bit integers in Java, C++ and C#.
- Running the loop for n = 1 or 2 with arrays sized n + 1 and writing full[2] out of bounds. Handle small n first.
Frequently asked questions
Why is full[0] equal to 1?
There is exactly one way to tile an empty board: place nothing. That value is what makes full[2] = full[1] + full[0] count the two-horizontal-dominoes case correctly.
Why does part not need two versions for the top and bottom row?
The board is symmetric top to bottom, so the number of tilings with the top cell sticking out equals the number with the bottom cell sticking out. One count plus the factor 2 is enough.
Where does full[n] = 2 * full[n-1] + full[n-3] come from?
Subtract the full[i] rule for i - 1 from the rule for i and substitute part[i-1] - part[i-2] = full[i-3]. The jagged state cancels out.
Is this similar to Climbing Stairs?
Yes. With dominoes only, the count is the Fibonacci sequence, exactly like climbing 1 or 2 steps. Trominoes add the jagged-edge state on top.