> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Domino and Tromino Tiling Python Solution

> Tested Python solution for LeetCode 790 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 790, [Medium](/catalog/medium). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/domino-and-tromino-tiling/description/).

Generate this problem as a practice environment: tested reference solution, 17 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 790   # by problem number
lcpy gen -s domino_and_tromino_tiling   # by problem name
```

## Problem

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

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** `10^9 + 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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/07/15/lc-domino.jpg)

```
Input: n = 3
Output: 5
Explanation: The five different ways are shown above.
```

```
Input: n = 1
Output: 1
```

### Constraints

* 1 \<= n \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/domino_and_tromino_tiling/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/domino_and_tromino_tiling/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def num_tilings(self, n: int) -> int:
        mod = 1_000_000_007
        # f(i): ways to fully tile a 2 x i board; recurrence f(i) = 2f(i-1) + f(i-3)
        f0, f1, f2 = 1, 1, 2  # f(0), f(1), f(2)
        if n <= 2:
            return (f0, f1, f2)[n]
        for _ in range(3, n + 1):
            f0, f1, f2 = f1, f2, (2 * f2 + f0) % mod
        return f2
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.