Domino and Tromino Tiling - Problem

You have two types of tiles:

  • A 2 × 1 domino shape
  • A tromino shape (L-shaped)

You may rotate these shapes. Given an integer n, return the number of ways to tile a 2 × 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.

Input & Output

Example 1 — Basic Case
$ Input: n = 3
Output: 5
💡 Note: There are 5 ways to tile a 2×3 board: (1) three vertical dominos, (2) one vertical + two horizontal, (3) two horizontal + one vertical, (4) two trominos, (5) different tromino arrangement
Example 2 — Minimum Case
$ Input: n = 1
Output: 1
💡 Note: Only one way to tile a 2×1 board: place one vertical domino
Example 3 — Small Even
$ Input: n = 2
Output: 2
💡 Note: Two ways to tile a 2×2 board: (1) two vertical dominos side by side, (2) two horizontal dominos stacked

Constraints

  • 1 ≤ n ≤ 1000

Visualization

Tap to expand
Domino and Tromino Tiling INPUT 2 x n Board (n = 3) Tile Types: Domino Tromino n = 3 Board width ALGORITHM (DP) 1 Define States dp[i] = ways to fill 2xi 2 Base Cases dp[1]=1, dp[2]=2, dp[3]=5 3 Recurrence dp[n]=2*dp[n-1]+dp[n-3] 4 Compute Return dp[n] mod 10^9+7 DP Table: i 1 2 3 dp[i] 1 2 5 5 unique tilings exist ... FINAL RESULT All 5 Valid Tilings: #1 #2 #3 #4 #5 Output: 5 OK - Verified! dp[3] = 5 Key Insight: The recurrence dp[n] = 2*dp[n-1] + dp[n-3] captures all tiling possibilities: - 2*dp[n-1]: Add vertical domino OR two horizontal dominoes to previous state - dp[n-3]: Account for L-shaped tromino combinations that span 3 columns TutorialsPoint - Domino and Tromino Tiling | Dynamic Programming Approach
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen