跳至主要內容

790. 多米诺和托米诺平铺


790. 多米诺和托米诺平铺

🟠   🔖  动态规划  🔗 力扣open in new window LeetCodeopen in new window

题目

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:

  • 1 <= n <= 1000

题目大意

有两种形状的瓷砖:一种是 2 x 1 的多米诺形,另一种是形如 "L" 的托米诺形。两种形状都可以旋转。

给定整数 n ,返回可以平铺 2 x n 的面板的方法的数量。返回对 109 + 7 取模 的值。

平铺指的是每个正方形都必须有瓷砖覆盖。两个平铺不同,当且仅当面板上有四个方向上的相邻单元中的两个,使得恰好有一个平铺有一个瓷砖占据两个正方形。

示例 1:

输入: n = 3

输出: 5

解释: 五种不同的方法如上所示。

示例 2:

输入: n = 1

输出: 1

提示:

  • 1 <= n <= 1000

解题思路

复杂度分析

  • 时间复杂度O()
  • 空间复杂度O()

代码