From 0d6b44926e22b74a47557f2a7c1666194d3a83bd Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:14:31 +0530 Subject: [PATCH 1/6] Create 790. Domino and Tromino Tiling.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../790. Domino and Tromino Tiling.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/790. Domino and Tromino Tiling/790. Domino and Tromino Tiling.py diff --git a/Solution/790. Domino and Tromino Tiling/790. Domino and Tromino Tiling.py b/Solution/790. Domino and Tromino Tiling/790. Domino and Tromino Tiling.py new file mode 100644 index 0000000..e69de29 From 7c3aa10a3816742a450fbb2e7809b662cd78d025 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:14:53 +0530 Subject: [PATCH 2/6] Update 790. Domino and Tromino Tiling.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../790. Domino and Tromino Tiling.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Solution/790. Domino and Tromino Tiling/790. Domino and Tromino Tiling.py b/Solution/790. Domino and Tromino Tiling/790. Domino and Tromino Tiling.py index e69de29..a73c4e9 100644 --- a/Solution/790. Domino and Tromino Tiling/790. Domino and Tromino Tiling.py +++ b/Solution/790. Domino and Tromino Tiling/790. Domino and Tromino Tiling.py @@ -0,0 +1,12 @@ +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] \ No newline at end of file From 5ac4cba2b5405f4993473f075ae46aadc602018c Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:15:23 +0530 Subject: [PATCH 3/6] Create 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> --- Solution/790. Domino and Tromino Tiling/readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/790. Domino and Tromino Tiling/readme.md diff --git a/Solution/790. Domino and Tromino Tiling/readme.md b/Solution/790. Domino and Tromino Tiling/readme.md new file mode 100644 index 0000000..e69de29 From 0461ad348594b6f313f28b9b1ed09a5debf18d7d Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:15:29 +0530 Subject: [PATCH 4/6] 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> --- .../790. Domino and Tromino Tiling/readme.md | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) diff --git a/Solution/790. Domino and Tromino Tiling/readme.md b/Solution/790. Domino and Tromino Tiling/readme.md index e69de29..e7da669 100644 --- a/Solution/790. Domino and Tromino Tiling/readme.md +++ b/Solution/790. Domino and Tromino Tiling/readme.md @@ -0,0 +1,169 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0790.Domino%20and%20Tromino%20Tiling/README_EN.md +tags: + - Dynamic Programming +--- + + + +# [790. Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling) + +[中文文档](/solution/0700-0799/0790.Domino%20and%20Tromino%20Tiling/README.md) + +## Description + + + +

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 109 + 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.

+ +

 

+

Example 1:

+ +
+Input: n = 3
+Output: 5
+Explanation: The five different ways are show above.
+
+ +

Example 2:

+ +
+Input: n = 1
+Output: 1
+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1: Dynamic Programming + +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. + +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. + +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: + +- The last column is fully covered, denoted as $0$ +- The last column has only the top square covered, denoted as $1$ +- The last column has only the bottom square covered, denoted as $2$ +- The last column is not covered, denoted as $3$ + +The answer is $f[n][0]$. Initially, $f[0][0] = 1$ and the rest $f[0][j] = 0$. + +We consider tiling up to the $i$-th column and look at the state transition equations: + +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]$. + +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]$. + +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]$. + +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]$. + +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. + +Note that the values of the states can be very large, so we need to take modulo $10^9 + 7$. + +The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the number of columns of the board. + + + +#### Python3 + +```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] +``` + +#### Java + +```java +class Solution { + public int numTilings(int n) { + long[] f = {1, 0, 0, 0}; + int mod = (int) 1e9 + 7; + for (int i = 1; i <= n; ++i) { + long[] g = new long[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 (int) f[0]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + const int mod = 1e9 + 7; + + int numTilings(int n) { + long f[4] = {1, 0, 0, 0}; + for (int i = 1; i <= n; ++i) { + long g[4] = {0, 0, 0, 0}; + 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]; + memcpy(f, g, sizeof(g)); + } + return f[0]; + } +}; +``` + +#### Go + +```go +func numTilings(n int) int { + f := [4]int{} + f[0] = 1 + const mod int = 1e9 + 7 + for i := 1; i <= n; i++ { + g := [4]int{} + 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] +} +``` + + + + + + \ No newline at end of file From ab2bac037f399df19c2a0d21ad3ffb2a9d0d4773 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:16:00 +0530 Subject: [PATCH 5/6] 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> --- Solution/790. Domino and Tromino Tiling/readme.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Solution/790. Domino and Tromino Tiling/readme.md b/Solution/790. Domino and Tromino Tiling/readme.md index e7da669..8eb57aa 100644 --- a/Solution/790. Domino and Tromino Tiling/readme.md +++ b/Solution/790. Domino and Tromino Tiling/readme.md @@ -1,3 +1,7 @@ + + +# [790. Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling) + --- comments: true difficulty: Medium @@ -6,12 +10,6 @@ tags: - Dynamic Programming --- - - -# [790. Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling) - -[中文文档](/solution/0700-0799/0790.Domino%20and%20Tromino%20Tiling/README.md) - ## Description From 175b2a7e703011a43267cde858d36e82cde17649 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:16:46 +0530 Subject: [PATCH 6/6] 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> --- Solution/790. Domino and Tromino Tiling/readme.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Solution/790. Domino and Tromino Tiling/readme.md b/Solution/790. Domino and Tromino Tiling/readme.md index 8eb57aa..467b2a6 100644 --- a/Solution/790. Domino and Tromino Tiling/readme.md +++ b/Solution/790. Domino and Tromino Tiling/readme.md @@ -3,10 +3,9 @@ # [790. Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling) --- -comments: true -difficulty: Medium -edit_url: https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0790.Domino%20and%20Tromino%20Tiling/README_EN.md -tags: +- **comments**: true +- **difficulty**: Medium +- **tags**: - Dynamic Programming ---