Skip to content

Commit 0461ad3

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent 5ac4cba commit 0461ad3

File tree

1 file changed

+169
-0
lines changed
  • Solution/790. Domino and Tromino Tiling

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0790.Domino%20and%20Tromino%20Tiling/README_EN.md
5+
tags:
6+
- Dynamic Programming
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [790. Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling)
12+
13+
[中文文档](/solution/0700-0799/0790.Domino%20and%20Tromino%20Tiling/README.md)
14+
15+
## Description
16+
17+
<!-- description:start -->
18+
19+
<p>You have two types of tiles: a <code>2 x 1</code> domino shape and a tromino shape. You may rotate these shapes.</p>
20+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0790.Domino%20and%20Tromino%20Tiling/images/lc-domino.jpg" style="width: 362px; height: 195px;" />
21+
<p>Given an integer n, return <em>the number of ways to tile an</em> <code>2 x n</code> <em>board</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
22+
23+
<p>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.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0790.Domino%20and%20Tromino%20Tiling/images/lc-domino1.jpg" style="width: 500px; height: 226px;" />
28+
<pre>
29+
<strong>Input:</strong> n = 3
30+
<strong>Output:</strong> 5
31+
<strong>Explanation:</strong> The five different ways are show above.
32+
</pre>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> n = 1
38+
<strong>Output:</strong> 1
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= n &lt;= 1000</code></li>
46+
</ul>
47+
48+
<!-- description:end -->
49+
50+
## Solutions
51+
52+
<!-- solution:start -->
53+
54+
### Solution 1: Dynamic Programming
55+
56+
First, we need to understand the problem. The problem is essentially asking us to find the number of ways to tile a $2 \times n$ board, where each square on the board can only be covered by one tile.
57+
58+
There are two types of tiles: `2 x 1` and `L` shapes, and both types of tiles can be rotated. We denote the rotated tiles as `1 x 2` and `L'` shapes.
59+
60+
We define $f[i][j]$ to represent the number of ways to tile the first $2 \times i$ board, where $j$ represents the state of the last column. The last column has 4 states:
61+
62+
- The last column is fully covered, denoted as $0$
63+
- The last column has only the top square covered, denoted as $1$
64+
- The last column has only the bottom square covered, denoted as $2$
65+
- The last column is not covered, denoted as $3$
66+
67+
The answer is $f[n][0]$. Initially, $f[0][0] = 1$ and the rest $f[0][j] = 0$.
68+
69+
We consider tiling up to the $i$-th column and look at the state transition equations:
70+
71+
When $j = 0$, the last column is fully covered. It can be transitioned from the previous column's states $0, 1, 2, 3$ by placing the corresponding tiles, i.e., $f[i-1][0]$ with a `1 x 2` tile, $f[i-1][1]$ with an `L'` tile, $f[i-1][2]$ with an `L'` tile, or $f[i-1][3]$ with two `2 x 1` tiles. Therefore, $f[i][0] = \sum_{j=0}^3 f[i-1][j]$.
72+
73+
When $j = 1$, the last column has only the top square covered. It can be transitioned from the previous column's states $2, 3$ by placing a `2 x 1` tile or an `L` tile. Therefore, $f[i][1] = f[i-1][2] + f[i-1][3]$.
74+
75+
When $j = 2$, the last column has only the bottom square covered. It can be transitioned from the previous column's states $1, 3$ by placing a `2 x 1` tile or an `L'` tile. Therefore, $f[i][2] = f[i-1][1] + f[i-1][3]$.
76+
77+
When $j = 3$, the last column is not covered. It can be transitioned from the previous column's state $0$. Therefore, $f[i][3] = f[i-1][0]$.
78+
79+
We can see that the state transition equations only involve the previous column's states, so we can use a rolling array to optimize the space complexity.
80+
81+
Note that the values of the states can be very large, so we need to take modulo $10^9 + 7$.
82+
83+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the number of columns of the board.
84+
85+
<!-- tabs:start -->
86+
87+
#### Python3
88+
89+
```python
90+
class Solution:
91+
def numTilings(self, n: int) -> int:
92+
f = [1, 0, 0, 0]
93+
mod = 10**9 + 7
94+
for i in range(1, n + 1):
95+
g = [0] * 4
96+
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod
97+
g[1] = (f[2] + f[3]) % mod
98+
g[2] = (f[1] + f[3]) % mod
99+
g[3] = f[0]
100+
f = g
101+
return f[0]
102+
```
103+
104+
#### Java
105+
106+
```java
107+
class Solution {
108+
public int numTilings(int n) {
109+
long[] f = {1, 0, 0, 0};
110+
int mod = (int) 1e9 + 7;
111+
for (int i = 1; i <= n; ++i) {
112+
long[] g = new long[4];
113+
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod;
114+
g[1] = (f[2] + f[3]) % mod;
115+
g[2] = (f[1] + f[3]) % mod;
116+
g[3] = f[0];
117+
f = g;
118+
}
119+
return (int) f[0];
120+
}
121+
}
122+
```
123+
124+
#### C++
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
const int mod = 1e9 + 7;
130+
131+
int numTilings(int n) {
132+
long f[4] = {1, 0, 0, 0};
133+
for (int i = 1; i <= n; ++i) {
134+
long g[4] = {0, 0, 0, 0};
135+
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod;
136+
g[1] = (f[2] + f[3]) % mod;
137+
g[2] = (f[1] + f[3]) % mod;
138+
g[3] = f[0];
139+
memcpy(f, g, sizeof(g));
140+
}
141+
return f[0];
142+
}
143+
};
144+
```
145+
146+
#### Go
147+
148+
```go
149+
func numTilings(n int) int {
150+
f := [4]int{}
151+
f[0] = 1
152+
const mod int = 1e9 + 7
153+
for i := 1; i <= n; i++ {
154+
g := [4]int{}
155+
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod
156+
g[1] = (f[2] + f[3]) % mod
157+
g[2] = (f[1] + f[3]) % mod
158+
g[3] = f[0]
159+
f = g
160+
}
161+
return f[0]
162+
}
163+
```
164+
165+
<!-- tabs:end -->
166+
167+
<!-- solution:end -->
168+
169+
<!-- problem:end -->

0 commit comments

Comments
 (0)