|
| 1 | +--- |
| 2 | +id: domino-and-tromino-tiling |
| 3 | +title: Domino and Tromino Tiling |
| 4 | +sidebar_label: Domino and Tromino Tiling |
| 5 | +tags: [Dynamic Programming, Combinatorics, Math, C++, Python, Java] |
| 6 | +description: Solve the problem of finding the number of ways to tile a 2 x n board using 2 x 1 dominos and tromino shapes. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes. |
| 14 | + |
| 15 | +Given an integer `n`, return the number of ways to tile a 2 x `n` board. Since the answer may be very large, return it modulo $10^9 + 7$. |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | +``` |
| 23 | +Input: n = 3 |
| 24 | +Output: 5 |
| 25 | +``` |
| 26 | +**Explanation:** The five different ways are shown above. |
| 27 | + |
| 28 | + |
| 29 | +### Constraints |
| 30 | + |
| 31 | +- $1 \leq n \leq 1000$ |
| 32 | + |
| 33 | +## Solution |
| 34 | + |
| 35 | +### Intuition |
| 36 | + |
| 37 | +To solve this problem, we can use dynamic programming. Let's define `dp[i]` as the number of ways to tile a 2 x `i` board. The recurrence relation can be derived based on the ways we can place the last tile(s) on the board. |
| 38 | + |
| 39 | +### Time Complexity and Space Complexity Analysis |
| 40 | + |
| 41 | +- **Time Complexity**: The solution involves a single loop through the board length `n`, making the time complexity $O(n)$. |
| 42 | +- **Space Complexity**: The space complexity is $O(n)$ to store the dynamic programming array. |
| 43 | + |
| 44 | +### Code |
| 45 | + |
| 46 | +#### C++ |
| 47 | + |
| 48 | +```cpp |
| 49 | +class Solution { |
| 50 | +public: |
| 51 | + int numTilings(int n) { |
| 52 | + if (n == 1) return 1; |
| 53 | + if (n == 2) return 2; |
| 54 | + const int MOD = 1e9 + 7; |
| 55 | + vector<long> dp(n + 1); |
| 56 | + dp[0] = 1; |
| 57 | + dp[1] = 1; |
| 58 | + dp[2] = 2; |
| 59 | + for (int i = 3; i <= n; ++i) { |
| 60 | + dp[i] = (dp[i - 1] + dp[i - 2] + 2 * dp[i - 3]) % MOD; |
| 61 | + } |
| 62 | + return dp[n]; |
| 63 | + } |
| 64 | +}; |
| 65 | +``` |
| 66 | +#### Java |
| 67 | +
|
| 68 | +```java |
| 69 | +class Solution { |
| 70 | + public int numTilings(int n) { |
| 71 | + if (n == 1) return 1; |
| 72 | + if (n == 2) return 2; |
| 73 | + int MOD = 1000000007; |
| 74 | + long[] dp = new long[n + 1]; |
| 75 | + dp[0] = 1; |
| 76 | + dp[1] = 1; |
| 77 | + dp[2] = 2; |
| 78 | + for (int i = 3; i <= n; ++i) { |
| 79 | + dp[i] = (dp[i - 1] + dp[i - 2] + 2 * dp[i - 3]) % MOD; |
| 80 | + } |
| 81 | + return (int) dp[n]; |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | +#### Python |
| 86 | +```python |
| 87 | +class Solution: |
| 88 | + def numTilings(self, n: int) -> int: |
| 89 | + if n == 1: return 1 |
| 90 | + if n == 2: return 2 |
| 91 | + MOD = 10**9 + 7 |
| 92 | + dp = [0] * (n + 1) |
| 93 | + dp[0] = 1 |
| 94 | + dp[1] = 1 |
| 95 | + dp[2] = 2 |
| 96 | + for i in range(3, n + 1): |
| 97 | + dp[i] = (dp[i - 1] + dp[i - 2] + 2 * dp[i - 3]) % MOD |
| 98 | + return dp[n] |
| 99 | +``` |
0 commit comments