InterviewPrepKit

Home / Coding / Algorithm & Data Structure / 1-D Dynamic Programming

Domino and Tromino Tiling

medium Original ↗ 00:00

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 with no gaps and no overlaps. The count grows quickly, so 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.

With n up to 1000, an O(n) recurrence is sufficient. The difficulty is finding the recurrence, because trominoes leave partially filled columns.

Think about it first

Hint 1 Work out small cases by hand: `f(1)=1`, `f(2)=2`, `f(3)=5`, `f(4)=11`. The extra tilings beyond a pure-domino Fibonacci count come from the L-tiles, which can leave one column partially filled.
Hint 2 One closed recurrence is `f(n) = 2·f(n-1) + f(n-3)`. Check: `f(4) = 2·5 + 1 = 11`, `f(5) = 2·11 + 2 = 24`.
Hint 3 To derive the count rather than memorize it, track two states per column: `full[i]` = ways to tile the first `i` columns with a flush right edge, and `partial[i]` = ways to tile them with exactly one cell of column `i` filled. Transitions between `full` and `partial` produce the same sequence. Either way, keep the last few values and reduce mod `10^9 + 7`.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug