Domino and Tromino Tiling — LeetCode 790 Python Solution
MediumDynamic Programming
- Problem
- #790
- Pattern
- Dynamic Programming
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes.
Example
- Input
- n = 3
- Output
- 5
- Explanation
- The five different ways are shown above.
Python solution
Python
class Solution:
def numTilings(self, n: int) -> int:
f = [1, 0, 0, 0]
mod = 10**9 + 7
for i in range(1, n + 1):
g = [0] * 4
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod
g[1] = (f[2] + f[3]) % mod
g[2] = (f[1] + f[3]) % mod
g[3] = f[0]
f = g
return f[0]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) auxiliary |
Pattern: Dynamic Programming
Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 790. Domino and Tromino Tiling is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.
The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.
Related problems
On a study list
This problem is on LeetCode 75.
Frequently asked questions
- How hard is LeetCode 790. Domino and Tromino Tiling?
- LeetCode 790. Domino and Tromino Tiling is rated Medium on LeetCode.
- What is the time complexity of LeetCode 790. Domino and Tromino Tiling?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 790. Domino and Tromino Tiling?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 790. Domino and Tromino Tiling cover?
- LeetCode 790. Domino and Tromino Tiling is tagged Dynamic Programming on LeetCode.