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].

Approach #1: DP. [C++]

class Solution {
public:
int numTilings(int N) {
constexpr int mod = 1000000007;
vector<vector<long>> dp(N+1, vector<long>(2, 0)); dp[0][0] = dp[1][0] = 1; for (int i = 2; i <= N; ++i) {
dp[i][0] = (dp[i-1][0] + dp[i-2][0] + 2 * dp[i-1][1]) % mod;
dp[i][1] = (dp[i-2][0] + dp[i-1][1]) % mod;
} return dp[N][0];
}
};

  

Analysis:

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-790-domino-and-tromino-tiling/

790. Domino and Tromino Tiling的更多相关文章

  1. leetcode 790. Domino and Tromino Tiling

    We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...

  2. 【LeetCode】790. Domino and Tromino Tiling 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/domino-a ...

  3. [Swift]LeetCode790. 多米诺和托米诺平铺 | Domino and Tromino Tiling

    We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...

  4. [LeetCode] Domino and Tromino Tiling 多米诺和三格骨牌

    We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...

  5. 73th LeetCode Weekly Contest Domino and Tromino Tiling

    We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...

  6. 动态规划-填格子问题 Domino and Tromino Tiling

    2018-09-01 22:38:19 问题描述: 问题求解: 本题如果是第一看到,应该还是非常棘手的,基本没有什么思路. 不妨先从一种简化的版本来考虑.如果仅有一种砖块,那么,填充的方式如下.

  7. leetcode790 Domino and Tromino Tiling

    思路: dp.没有像discuss中的那样优化递推式. 实现: class Solution { public: ; int numTilings(int N) { vector<vector& ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

随机推荐

  1. [python] can not find app ,module

    can not find module 1 startapp appname 而不是 startproject 2 不要自己创建项目根目录,要用mamage.py生成 can not find app ...

  2. g++报错原因分析:expected class-name before ‘{’ token

    今天写程序的时候, 遇到这样一个错误expected class-name before ‘{’ token 最后发现原来是我的头文件声明没有加. 继承时不要忘记加基类的头文件 错误: class F ...

  3. oracle 存储过程应用

    1.查看 SELECT * FROM all_source WHERE type='PROCEDURE' and name=upper('liuyi_prcd'); 2.删除 DROP PROCEDU ...

  4. struct的使用

    编写一个学生struct,成员有学号(id).姓名(name).成绩(5门课程),随机生成多个学生的学号.姓名和成绩存储到结构体数组.再根据总分进行排名并输出学生的信息和总分. stu.h #ifnd ...

  5. svn 创建tag

    1. 右键项目(源) 2. 选择复制到哪个路径(创建文件夹选项) 3. 选择哪个版本(源的) 4. 填写备注 5. 结束 使用:import  .合并等

  6. 命令行中开启mySQL数据库服务

    sudo /Applications/XAMPP/xamppfiles/bin/mysql.server start  

  7. NSData与UIImage之间的转换

    1 //NSData转换为UIImage 2 NSData *imageData = [NSData dataWithContentsOfFile: imagePath]; 3 UIImage *im ...

  8. id 与 void * 转换

    MRC 环境下: id 变量赋值给 void * 变量运行时不会有问题. id obj1 = [NSObject new];void * p = obj1; void * 变量赋值给 id 变量并调用 ...

  9. Servlet从浅入深

    Servlet是什么 servlet 是运行在 Web 服务器中的小型 Java 程序(即:服务器端的小应用程序). servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户端 ...

  10. (最小生成树)QS Network -- ZOJ --1586

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...