Domino and Tromino Tiling
Problem
Given an integer n, return the number of ways to tile a 2 x n board using 2 x 1 dominoes and L-shaped trominoes, modulo 10^9 + 7.
- 1 ≤ n ≤ 1000
Example
n = 35The possible tilings for a 2 x 3 board are: 1. Three vertical dominoes. 2. One horizontal domino on top row and two vertical dominoes below. 3. One horizontal domino on bottom row and two vertical dominoes above. 4. Two horizontal dominoes stacked. 5. One L-shaped tromino and one vertical domino. The algorithm uses a DP array to count ways for smaller boards and builds up to n using the recurrence dp[i] = 2 * dp[i-1] + dp[i-3].
Approach
Straightforward Solution
A brute-force approach would try all possible placements recursively, enumerating all tilings, which is exponential and infeasible for large n.
Core Observation
The number of ways to tile a 2 x n board depends on the ways to tile smaller boards, with the key insight that placing a vertical domino or tromino affects the board differently, leading to a recurrence involving dp[i-1] and dp[i-3].
Path to Optimal
Recognizing the problem as a counting problem with overlapping subproblems suggests dynamic programming. The key insight is to derive a recurrence relation that expresses dp[i] in terms of dp[i-1] and dp[i-3], capturing the ways to extend smaller boards by adding dominoes or trominoes. This reduces the exponential search space to linear time.
Optimal Approach
Use a DP array dp where dp[i] represents the number of ways to tile a 2 x i board. Initialize base cases for dp[0], dp[1], and dp[2]. For i >= 3, compute dp[i] = 2 * dp[i-1] + dp[i-3], taking modulo 10^9 + 7 to handle large numbers. Return dp[n] as the final answer.
Time
O(n)
The algorithm computes dp values from 0 up to n once, each in constant time, resulting in linear time complexity.
Space
O(n)
The dp array stores n+1 integers, each representing the count of tilings for boards of length up to n, which is necessary to avoid recomputation.
Pattern Spotlight
Dynamic Programming (Linear Recurrence with State Compression)
When counting combinatorial configurations with overlapping subproblems and complex piece placements, identify a recurrence that relates the current state to a fixed number of previous states, enabling efficient bottom-up computation.
Solution
| 1 | class Solution: |
| 2 | def numTilings(self, n: int) -> int: |
| 3 | MOD = 10**9 + 7 |
| 4 | |
| 5 | if n == 1: |
| 6 | return 1 |
| 7 | |
| 8 | if n == 2: |
| 9 | return 2 |
| 10 | |
| 11 | dp = [0] * (n + 1) |
| 12 | dp[0] = 1 |
| 13 | dp[1] = 1 |
| 14 | dp[2] = 2 |
| 15 | |
| 16 | for i in range(3, n + 1): |
| 17 | dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD |
| 18 | |
| 19 | return dp[n] |
Step-by-Step Solution
Handle Base Cases for Small Board Lengths
| 5 | if n == 1: |
| 6 | return 1 |
| 8 | if n == 2: |
| 9 | return 2 |
Objective
To return immediate results for trivial board lengths where the number of tilings is known directly.
Key Insight
For n=1 and n=2, the number of tilings is small and can be enumerated manually. Handling these cases separately avoids unnecessary DP computation and sets the foundation for the recurrence to build upon.
Interview Quick-Check
Core Logic
Returning known results for n=1 and n=2 prevents invalid DP indexing and provides correct base values.
State & Boundaries
Base cases ensure the DP recurrence has valid starting points for indices dp[0], dp[1], and dp[2].
Initialize DP Array with Base Values
| 11 | dp = [0] * (n + 1) |
| 12 | dp[0] = 1 |
| 13 | dp[1] = 1 |
| 14 | dp[2] = 2 |
Objective
To set up the DP array and assign initial counts for the smallest board lengths.
Key Insight
The DP array stores the number of tilings for each board length. Initializing dp[0] = 1 (empty board), dp[1] = 1, and dp[2] = 2 captures the base cases from which the recurrence can build solutions for larger boards.
Interview Quick-Check
Core Logic
dp[0] = 1 represents the empty board as a valid base case, enabling the recurrence to work correctly.
Common Pitfalls & Bugs
Incorrect base initialization leads to wrong counts or index errors during DP computation.
Compute Number of Tilings Using Recurrence Relation
| 16 | for i in range(3, n + 1): |
| 17 | dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD |
Objective
To iteratively compute the number of tilings for boards of length 3 to n using the derived recurrence.
Key Insight
The recurrence dp[i] = 2 * dp[i-1] + dp[i-3] captures all ways to extend smaller boards by adding dominoes or trominoes. Multiplying dp[i-1] by 2 accounts for two distinct placements extending from dp[i-1], while dp[i-3] accounts for tromino placements spanning three columns. Taking modulo ensures results fit within integer limits.
Interview Quick-Check
Core Logic
The recurrence relation efficiently counts all tilings by combining smaller subproblems without double counting.
Complexity
The loop runs in O(n) time, computing each dp[i] in constant time.
Common Pitfalls & Bugs
Forgetting to take modulo at each step can cause integer overflow and incorrect results.
Return Final Computed Result
| 19 | return dp[n] |
Objective
To output the total number of tilings for the full board length n.
Key Insight
After filling the DP array, dp[n] holds the count of all valid tilings modulo 10^9 + 7. Returning this value completes the solution.
Interview Quick-Check
Core Logic
Returning dp[n] provides the final answer computed by the DP process.
Line Analysis
This solution has 2 Critical lines interviewers watch for.
dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD
Compute dp[i] using the recurrence and modulo operation.
The recurrence dp[i] = 2 * dp[i-1] + dp[i-3] captures all tilings by extending smaller boards with dominoes and trominoes, and modulo ensures results stay within integer limits.
dp[0] = 1
Set dp[0] to 1 representing the empty board.
An empty board has one valid tiling (doing nothing), which serves as the base case for the recurrence.
if n == 1:
Check if n equals 1 and return the known number of tilings.
For a 2 x 1 board, there is exactly one way to tile it (a single vertical domino), so returning 1 immediately avoids unnecessary computation.
return 1
Return 1 for the base case n=1.
This early return handles the smallest input, ensuring correctness and preventing index errors in DP.
if n == 2:
Check if n equals 2 and return the known number of tilings.
For a 2 x 2 board, there are two tilings: two vertical dominoes or two horizontal dominoes, so returning 2 immediately sets the correct base case.
return 2
Return 2 for the base case n=2.
This early return handles the second smallest input, ensuring the DP recurrence has valid starting values.
dp[1] = 1
Set dp[1] to 1 representing the 2 x 1 board tilings.
This base case matches the known count for n=1, ensuring the recurrence can build correctly from this value.
dp[2] = 2
Set dp[2] to 2 representing the 2 x 2 board tilings.
This base case matches the known count for n=2, providing a foundation for the recurrence to compute larger values.
return dp[n]
Return the total number of tilings for board length n.
dp[n] holds the final computed count of tilings, completing the solution.
MOD = 10**9 + 7
Define the modulo constant to prevent integer overflow.
Using modulo 10^9 + 7 ensures that all intermediate and final results fit within 32-bit integer limits and conform to problem constraints requiring modular arithmetic.
for i in range(3, n + 1):
Iterate from 3 to n to fill dp array using recurrence.
This loop systematically computes the number of tilings for each board length by building on previously computed results, ensuring linear time complexity.
dp = [0] * (n + 1)
Initialize the DP array with zeros for indices 0 through n.
Allocating the DP array prepares storage for the number of tilings for all board lengths up to n, enabling bottom-up computation.
Test Your Understanding
Why does the recurrence dp[i] = 2 * dp[i-1] + dp[i-3] correctly count all tilings?
Because dp[i-1] accounts for extending the board by adding a vertical domino or an L-shaped tromino in two distinct ways (hence multiplied by 2), and dp[i-3] accounts for configurations where a tromino covers three columns, capturing all unique tilings without overlap or omission.
Related Problems
Dynamic Programming pattern
Don't just read it. Drill it.
Reconstruct Domino and Tromino Tiling from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.
Unlock the Domino and Tromino Tiling drill