InterviewPrepKit

Home / Coding / 1-D Dynamic Programming

Domino and Tromino Tiling

medium Original ↗
Solving tips
  • Compute f(1..4)=1,2,5,11 by hand to discover the closed recurrence f(n)=2*f(n-1)+f(n-3), or derive it with full/partial column states.
  • The tromino leaves a one-cell overhang, so if deriving from scratch track two states: flush edge vs one-cell jut-out.
  • Reduce modulo 10^9+7 at every step, not just at the end, since f(1000) is enormous.
  • Target O(n) time, O(1) space with rolling scalars; base f(0)=1 is what makes f(3)=5 come out right.

Problem

You have two tile shapes:

  • a domino: a 2 × 1 (or 1 × 2) rectangle, and
  • a tromino: an L-shaped tile covering three cells (a 2 × 2 square with one corner removed).

Both may be rotated. Count the number of ways to completely tile a 2 × n board, leaving no gaps and no overlaps. Because the count grows fast, return it modulo 10^9 + 7.

Examples

  • n = 11 — only a single vertical domino fits the 2 × 1 board.
  • n = 35 — the five distinct tilings of a 2 × 3 board.
  • n = 411.

Constraints

  • 1 <= n <= 1000
  • Answer is returned modulo 10^9 + 7.

n up to 1000 means an O(n) recurrence is more than enough; the real work is finding the recurrence, because trominoes create partially-filled columns.

Think about it first

Hint 1 Try small cases by hand: `f(1)=1`, `f(2)=2`, `f(3)=5`, `f(4)=11`. The jump from a pure-domino Fibonacci-like count comes from the L-tiles leaving a "staircase" edge.
Hint 2 One clean closed recurrence is `f(n) = 2·f(n-1) + f(n-3)`. Sanity check: `f(4) = 2·5 + 1 = 11`, `f(5) = 2·11 + 2 = 24`. It captures "extend the previous board" plus the new arrangements a tromino pair unlocks.
Hint 3 If you prefer to *derive* rather than memorize, track two states per column: `full[i]` = ways to tile the first `i` columns flush, and `partial[i]` = ways to tile them with exactly one cell of column `i` jutting out. Transitions between `full` and `partial` give the same sequence. Either way, keep the last few values and reduce mod `10^9 + 7`.
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.