LeetCode //C - 790. Domino and Tromino Tiling

790. Domino and Tromino Tiling

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 1 0 9 + 7 10^9 + 7 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:
  • 1 <= n <= 1000

From: LeetCode
Link: 790. Domino and Tromino Tiling


Solution:

Ideas:
  1. Define a recurrence relation to calculate the number of tilings for a board of width n.
  2. The base cases will be small widths for which we can manually count the number of tilings.
  3. For larger widths, we build up the solution from the base cases, using the recurrence relation.
  4. We need to consider the last column which could be filled by:
    • A vertical domino, which leaves the subproblem of tiling a 2 x (n-1) board.
    • Two horizontal dominos, which leaves the subproblem of tiling a 2 x (n-2) board.
    • A tromino along with a domino, which will lead to two subproblems: tiling a 2 x (n-2) board and a 2 x (n-3) board.
  5. Since the answer can be very large, we will return it modulo 1 0 9 + 7 10^9+7 109+7.
Caode:
int numTilings(int n) {
   
    if (n == 1) return 1;
    if (n == 2) return 2;
    if (n == 3) return 5;

    long dp[n+1];
    dp[0] = 1; dp[1] = 1; dp[2] = 2; dp[3] = 5;

    for (int i = 4; i <= n; ++i) {
   
        dp[i] = (2 * dp[i-1] % 1000000007 + dp[i-3]) % 1000000007; // Main recurrence relation
    }

    return (int) dp[n];
}

相关推荐

  1. MySQL商城数据表(70-79

    2024-02-12 16:36:01       29 阅读
  2. 797. 差分

    2024-02-12 16:36:01       55 阅读
  3. ZYNQ-700呼吸灯

    2024-02-12 16:36:01       36 阅读
  4. [leetcode] 796. 旋转字符串

    2024-02-12 16:36:01       38 阅读
  5. day<span style='color:red;'>70</span>

    day70

    2024-02-12 16:36:01      47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-12 16:36:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-12 16:36:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-12 16:36:01       87 阅读
  4. Python语言-面向对象

    2024-02-12 16:36:01       97 阅读

热门阅读

  1. Leetcode 300 最长递增子序列

    2024-02-12 16:36:01       55 阅读
  2. Leetcode 3036. Number of Subarrays That Match a Pattern II

    2024-02-12 16:36:01       69 阅读
  3. C# Avalonia 折线图

    2024-02-12 16:36:01       59 阅读
  4. 倒计时56天

    2024-02-12 16:36:01       58 阅读
  5. 100条安全原则来制定安全策略

    2024-02-12 16:36:01       52 阅读
  6. Jedis

    Jedis

    2024-02-12 16:36:01      56 阅读
  7. tsgctf-2021-lkgit-无锁竞争-userfaultfd

    2024-02-12 16:36:01       44 阅读
  8. Oracle恢复数据库某张表某一时刻的数据

    2024-02-12 16:36:01       49 阅读