Quick Overview

Count the ways to tile a 2 x n board with dominoes and L-shaped trominoes, any of which may be rotated, and return the count modulo 10^9 + 7. The problem tests whether you can derive a linear dynamic programming recurrence from how a tiling can end at the right edge.

Count the ways to tile a 2 x n board with dominoes and L-shaped trominoes

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You have two kinds of tiles: a `2 x 1` domino and an L-shaped tromino, which covers three cells of a `2 x 2` square. You may rotate both shapes, so a domino can lie vertically or horizontally and a tromino can face any of four directions. Given an integer `n`, return the number of ways to tile a `2 x n` board. In a tiling every cell is covered by exactly one tile. Since the answer may be very large, return it modulo `10^9 + 7`. ### Function Signature ```python def num_tilings(n: int) -> int: ``` ### Rules - 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 covers both cells with the same tile. - Return the count modulo `10^9 + 7`. ### Constraints - `1 <= n <= 1000` ### Examples ```text Input: n = 3 Output: 5 +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ | A | B | C | | A | A | B | | A | B | B | | A | A | B | | A | B | B | +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ | A | B | C | | C | C | B | | A | C | C | | A | B | B | | A | A | B | +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ ``` Cells with the same letter belong to one tile. Three tilings use only dominoes, and the other two use a pair of interlocking trominoes that fill the whole board in one of two mirror-image ways. ```text Input: n = 1 Output: 1 ``` Only one vertical domino fits in a single column. ```text Input: n = 4 Output: 11 ``` Five tilings use only dominoes. Four more put a two-tromino `2 x 3` block (in either mirror form) next to a vertical domino on the left or on the right, and the last two place a tromino at each end with one horizontal domino between them.

Overview: Count the ways to tile a 2 x n board with dominoes and L-shaped trominoes, any of which may be rotated, and return the count modulo 10^9 + 7. The problem tests whether you can derive a linear dynamic programming recurrence from how a tiling can end at the right edge.

You have two kinds of tiles: a `2 x 1` domino and an L-shaped tromino that covers three cells of a `2 x 2` square. Both shapes may be rotated, so a domino can lie vertically or horizontally and a tromino can face any of the four directions. Given an integer `n`, return the number of ways to tile a board with 2 rows and `n` columns. In a tiling, every cell is covered by exactly one tile. The count grows very fast, so return it modulo `10^9 + 7`. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the two tilings covers both of those cells with the same tile. Implement `num_tilings(n)`. It returns one integer from 0 to 1000000006, the tiling count reduced modulo `10^9 + 7`. That result always fits in a 32-bit signed integer. The exact count passes 2^31-1 long before `n = 1000`, and any value your code forms before reducing it can also pass 2^31-1, so keep such intermediate values in 64-bit integers (`long` in Java, `long long` in C++). ### Example 1 ```text Input: n = 3 Output: 5 ``` Three tilings use only dominoes: three vertical dominoes; two stacked horizontal dominoes on the left with a vertical domino on the right; and a vertical domino on the left with two stacked horizontal dominoes on the right. The other two tilings fill the whole board with a pair of interlocking trominoes, in one of two mirror-image ways. ### Example 2 ```text Input: n = 4 Output: 11 ``` Five tilings use only dominoes. Four more place a two-tromino `2 x 3` block (in either mirror form) beside a vertical domino on its left or its right. The last two put a tromino at each end with one horizontal domino between them. ### Constraints - `1 <= n <= 1000` - The answer is returned modulo `10^9 + 7`, so it is an integer from 0 to 1000000006.

Constraints

  • 1 <= n <= 1000
  • The answer is returned modulo 10^9 + 7, so it is an integer from 0 to 1000000006.

Examples

Input: (1,)

Expected Output: 1

Explanation: Minimum width from the source example: a single vertical domino is the only tiling.

Input: (2,)

Expected Output: 2

Explanation: Two vertical dominoes or two stacked horizontal dominoes; a tromino pair needs at least 3 columns.

Hints

  1. Work out the answers for widths 1 to 4 by hand (1, 2, 5 and 11) and look at what can occupy the rightmost columns of a finished tiling.
  2. A tromino leaves one empty cell inside its 2 x 2 square. Follow what has to fill that cell, and the cell after it, until the shape closes off again.
  3. Reduce modulo 10^9 + 7 as you go, since the exact count for n = 1000 has hundreds of digits.

Count 2 x n Board Tilings with Dominoes and Trominoes

mediumAmazonSoftware Engineer

You have two kinds of tiles: a 2 x 1 domino and an L-shaped tromino that covers three cells of a 2 x 2 square. Both shapes may be rotated, so a domino can lie vertically or horizontally and a tromino can face any of the four directions.

Given an integer n, return the number of ways to tile a board with 2 rows and n columns. In a tiling, every cell is covered by exactly one tile. The count grows very fast, so return it modulo 10^9 + 7.

Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the two tilings covers both of those cells with the same tile.

Implement num_tilings(n). It returns one integer from 0 to 1000000006, the tiling count reduced modulo 10^9 + 7. That result always fits in a 32-bit signed integer. The exact count passes 2^31-1 long before n = 1000, and any value your code forms before reducing it can also pass 2^31-1, so keep such intermediate values in 64-bit integers (long in Java, long long in C++).

Example 1

Input: n = 3
Output: 5

Three tilings use only dominoes: three vertical dominoes; two stacked horizontal dominoes on the left with a vertical domino on the right; and a vertical domino on the left with two stacked horizontal dominoes on the right. The other two tilings fill the whole board with a pair of interlocking trominoes, in one of two mirror-image ways.

Example 2

Input: n = 4
Output: 11

Five tilings use only dominoes. Four more place a two-tromino 2 x 3 block (in either mirror form) beside a vertical domino on its left or its right. The last two put a tromino at each end with one horizontal domino between them.

Constraints

  • 1 <= n <= 1000
  • The answer is returned modulo 10^9 + 7, so it is an integer from 0 to 1000000006.

Examples

Example 1
Input
(1,)
Output
1
Notes
Minimum width from the source example: a single vertical domino is the only tiling.
Example 2
Input
(2,)
Output
2
Notes
Two vertical dominoes or two stacked horizontal dominoes; a tromino pair needs at least 3 columns.

Constraints

  • •1 <= n <= 1000
  • •The answer is returned modulo 10^9 + 7, so it is an integer from 0 to 1000000006.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def num_tilings(n: int) -> int:
Sample call
num_tilings(1)
Language
Case 1
Input
(1,)
Expected
1
Case 2
Input
(2,)
Expected
2

Browse More Questions

Show the approach

Approach

Let f(i) be the number of tilings of a 2 x i board, with f(0) = 1 for the empty board. In any tiling, take the rightmost vertical line short of the right edge that crosses no tile. The part to its right is a block that cannot be split by a clean vertical cut. A width 1 block is one vertical domino and a width 2 block is two stacked horizontal dominoes, one form each. A tromino leaves a one-cell notch in its 2 x 2 square, so a block that starts with a tromino continues with horizontal dominoes that alternate between the rows until a second tromino closes it; for every width k of 3 or more this gives exactly two blocks, one per mirror form. Summing over the last block gives f(i) = f(i - 1) + f(i - 2) + 2 * (f(i - 3) + ... + f(0)). Subtracting the same identity written for i - 1 collapses it to f(i) = 2 * f(i - 1) + f(i - 3) for i >= 3, with f(0) = 1, f(1) = 1 and f(2) = 2, which reproduces 5 for n = 3 and 11 for n = 4. The code keeps only the last three values. Before step i the variables hold f(i - 3), f(i - 2) and f(i - 1), and the step shifts them and appends f(i), so after the loop the last variable holds f(n). Reducing each new value modulo 10^9 + 7 is safe because the recurrence uses only addition and multiplication by a constant. For n = 1 and n = 2 the function returns 1 and 2 without entering the loop. Each new value 2 * f(i - 1) + f(i - 3) is computed from reduced values and stays below 3 * (10^9 + 7), which can overflow a 32-bit int, so Java and C++ form it in 64-bit integers, while JavaScript stays exact because the value is far below 2^53. The returned residue is at most 1000000006 and fits in an int.

Time complexity:
O(n)
Space complexity:
O(1)