Browse curriculum

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.

  1. 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).
  2. Boards with one cell sticking out end with a flat domino or a tromino, which ties them back to full boards.
  3. 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

1

0

1

1

2

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

Free preview: the first 3 of 7 steps

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

The approach, step by step

  1. Base cases

    dp[0] = 1, dp[1] = 1, dp[2] = 2.

  2. Fill

    For i from 3 to n, dp[i] = (2 · dp[i − 1] + dp[i − 3]) mod 10⁹ + 7.

  3. Answer

    Return dp[n].

Frequently asked questions

Where does dp[n] = 2 · dp[n − 1] + dp[n − 3] come from?

Track two counts: f(n) for full 2 × n boards and g(n) for boards with one extra cell sticking out. A full board ends with an upright domino (f(n − 1)), two flat dominoes (f(n − 2)), or a tromino closing a jagged edge, in two mirror images (2 · g(n − 1)). A jagged board ends with a flat domino on the jagged side (g(n − 1)) or a tromino (f(n − 2)). Subtracting the f equation for n − 1 from the one for n and substituting g leaves f(n) = 2 · f(n − 1) + f(n − 3).


Why is dp[0] = 1?

There is exactly one way to tile an empty board: place nothing. It also makes the recurrence give the right answer at n = 3: 2 · dp[2] + dp[0] = 2 · 2 + 1 = 5, which matches LeetCode example 1.


Why take the result modulo 10⁹ + 7?

The count grows a little faster than doubling: dp[28] is already over 10⁹, and n goes up to 1,000. Reducing modulo 10⁹ + 7 after every step keeps the numbers in range; 2 · dp[i − 1] + dp[i − 3] stays below 3.1 × 10⁹, which fits a 64-bit integer.


Can the space be reduced?

Yes. Each value only needs the previous three, so three variables can replace the array, giving O(1) space, the same trick as in N-th Tribonacci Number.

New to the technique behind this problem? Watch it run step by step:

Dynamic Programming Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description