【LeetCode】790. Domino and Tromino Tiling 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/domino-and-tromino-tiling/description/
题目描述:
We have two types of tiles: a 2x1 domino shape, and an “L” tromino shape. These shapes may be rotated.
XX <- domino
XX <- "L" tromino
X
Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 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:
Input: 3
Output: 5
Explanation:
The five different ways are listed below, different letters indicates different tiles:
XYZ XXZ XYY XXY XYY
XYZ YYZ XZZ XYY XXY
Note:
- N will be in range [1, 1000].
题目大意
有个2N的长条,在里面堆放两种骨牌:一种是12的长方形,另一种是L形的,均有无限多个。问总的有多少种堆叠方式。
解题方法
看到要模一个数,说明结果很大,肯定需要使用DP求解。这个DP的转移方程不好找。下面的分析来自花花酱,
先找一下规律,如果只有一种长方形的长条的话,那么递推公式是这样的:

当有两种长条的时候,同样的道理能得到这种状态的递推公式。

更详细的讲解要看花花酱的视频,我就不班门弄斧了。
时间复杂度是O(N),空间复杂度是O(N)。
class Solution:
def numTilings(self, N):
"""
:type N: int
:rtype: int
"""
dp = [[0] * 2 for _ in range(N + 1)]
dp[0][0] = 1
dp[1][0] = 1
for i in range(2, N + 1):
dp[i][0] = (dp[i - 1][0] + dp[i - 2][0] + 2 * dp[i - 1][1]) % (10 ** 9 + 7)
dp[i][1] = (dp[i - 2][0] + dp[i - 1][1]) % (10 ** 9 + 7)
return dp[-1][0]
参考资料:
https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-790-domino-and-tromino-tiling/
https://www.youtube.com/watch?v=S-fUTfqrdq8
日期
2018 年 10 月 15 日 —— 美好的周一怎么会出现雾霾呢?
【LeetCode】790. Domino and Tromino Tiling 解题报告(Python)的更多相关文章
- leetcode 790. Domino and Tromino Tiling
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...
- 790. Domino and Tromino Tiling
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
随机推荐
- python12类
self 表示类里面的对象的引用 python一般不需要去解决内存管理,解释器会进行自动给回收 #类鱼类之间空格两行,前面的函数里面也是两行,类里面的方法个一行 class Cat(object): ...
- 技术管理进阶——Leader的模型、手段及思维
这里可以添加关注交流一下嘛-- 本文更多的是个人认知,有不足请批评. Case 在之前一次年底考评的时候,有一位leader将一个案例同时用到了自己和下属身上,老板发出了责问: 这个项目到底你是负责 ...
- 用前端表格技术构建医疗SaaS 解决方案
电子健康档案(Electronic Health Records, EHR)是将患者在所有医疗机构产生的数据(病历.心电图.医疗影像等)以电子化的方式存储,通过在不同的医疗机构之间共享,让患者面对不同 ...
- 从源码看Thread&ThreadLocal&ThreadLocalMap的关系与原理
1.三者的之间的关系 ThreadLocalMap是Thread类的成员变量threadLocals,一个线程拥有一个ThreadLocalMap,一个ThreadLocalMap可以有多个Threa ...
- Linux基础命令---mysql
mysql mysql是一个简单的sql shell,它可以用来管理mysql数据库. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 m ...
- Linux:-e、-d、-f、-L、-r、-w、-x、-s、-h、
-e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fil ...
- java实现链式线性表
package ch9; public class LinkList <T>{ private class Node { //保存节点的数据 private T data; //指向下一个 ...
- easyhadoop 安装
ldconfig deferred processing now taking place正在处理用于 libapache2-mod-php5 的触发器... * Reloading web serv ...
- 【编程思想】【设计模式】【创建模式creational】lazy_evaluation
Python版 https://github.com/faif/python-patterns/blob/master/creational/lazy_evaluation.py #!/usr/bin ...
- Non-terminating decimal expansion; no exact representable decimal result.
Non-terminating decimal expansion; no exact representable decimal result. 翻译为:非终止十进制扩展; 没有确切的可表示的小数 ...