Post

806 Domino And Tromino Tiling

806 Domino And Tromino Tiling

Domino and Tromino Tiling image

You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes.

image

Given an integer n, return the number of ways to tile an 2 x 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.

 

Example 1:

image

1
2
3
4
5
**Input:** n = 3
**Output:** 5
**Explanation:** The five different ways are show above.

Example 2:

1
2
3
4
**Input:** n = 1
**Output:** 1

 

Constraints:

1
1 <= n <= 1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

class Solution:
    def numTilings(self, n: int) -> int:
        MOD = 10**9 + 7
        dp = [0] * (n + 1)
        dp[0] = 1
        if n >= 1:
            dp[1] = 1
        if n >= 2:
            dp[2] = 2

        for i in range(3, n + 1):
            dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD

        return dp[n]




This post is licensed under CC BY 4.0 by the author.