MediumNo. 0790
Domino and Tromino Tiling — LeetCode 790 Python Solution
- Pattern
- Dynamic Programming
- Topics
- Dynamic Programming
- On a study list
- LeetCode 75
- 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
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
- Time
- O(n)
- Space · auxiliary
- O(1)
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
| No. | Problem | Difficulty |
|---|---|---|
| 0788 | Rotated Digits | Medium |
| 0787 | Cheapest Flights Within K Stops | Medium |
| 0799 | Champagne Tower | Medium |
| 0010 | Regular Expression Matching | Hard |
| 0022 | Generate Parentheses | Medium |
| 0032 | Longest Valid Parentheses | Hard |
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.