Weekly Contest 73 leetcode 790. Domino and Tromino Tiling
2018-02-25 16:37
344 查看
We have two types of tiles(地砖): a 2x1 domino shape, and an "L" tromino shape. These shapes may be rotated.
Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 7.
Note:
N will be in range
这道题很明显是一道 DP 题。因此关键在于找到 DP 的递归公式。
方法是找规律。
序列如下:
我们发现:
使用DP的思路来考虑,为什么会出现这个公式呢?
A
can be get from A[N-1] by simply add
X
X
to the right of A[N-1],
A
can be get from A[N-2] by simply add
XX
YY
to the right of A[N-2] , Note that the method must be unique from those from A[N-1], thus the following tilling is not allowed:
XY
XY
cause it will be replicate to the cases from A[N-1] to A
How about A[N-3] and all previous cases? There are always 2 unique ways to turn them into A
(add the following blocks to the right of A[N-i]):
1.
XXY (from A[N-3])
XYY
XXYY (from A[N-4])
XZZY
XXZZY (from A[N-5])
XZZYY
XXZZYY (from A[N-6])
XZZZZY
etc…
2. vertically flip case 1(垂直反转case1)
Thus:
A
= A[N-1] + A[N-2] + 2 * sum(A[0:N-2])
重点在于:We can find dp[n-1] and dp[n-2] have one way to translate to dp
, and dp[n-3] ... dp[0] have two ways to translate to dp
. 然后就是推导公式,化简过程。
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.
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].
这道题很明显是一道 DP 题。因此关键在于找到 DP 的递归公式。
方法是找规律。
序列如下:
1, 1, 2, 5, 11, 24, 53, 117, 258, 569, 1255
我们发现:
5 = 2 * 2 + 1
11 = 5 * 2 + 1
24 = 11 * 2 + 2
53 = 24 * 2 + 5
117 = 57 * 2 + 11
A = A[N-1] * 2 + A[N-3]
使用DP的思路来考虑,为什么会出现这个公式呢?
A
can be get from A[N-1] by simply add
X
X
to the right of A[N-1],
A
can be get from A[N-2] by simply add
XX
YY
to the right of A[N-2] , Note that the method must be unique from those from A[N-1], thus the following tilling is not allowed:
XY
XY
cause it will be replicate to the cases from A[N-1] to A
How about A[N-3] and all previous cases? There are always 2 unique ways to turn them into A
(add the following blocks to the right of A[N-i]):
1.
XXY (from A[N-3])
XYY
XXYY (from A[N-4])
XZZY
XXZZY (from A[N-5])
XZZYY
XXZZYY (from A[N-6])
XZZZZY
etc…
2. vertically flip case 1(垂直反转case1)
Thus:
A
= A[N-1] + A[N-2] + 2 * sum(A[0:N-2])
重点在于:We can find dp[n-1] and dp[n-2] have one way to translate to dp
, and dp[n-3] ... dp[0] have two ways to translate to dp
. 然后就是推导公式,化简过程。
package leetcode; public class Domino_and_Tromino_Tiling_790 { public int numTilings(int N) { int mod=1000000007; long[] dp=new long[1001]; dp[1]=1; dp[2]=2; dp[3]=5; for(int i=4;i<=N;++i){ dp[i]=2*dp[i-1]+dp[i-3]; dp[i]=dp[i]%mod; } return (int)dp ; } public static void main(String[] args) { // TODO Auto-generated method stub Domino_and_Tromino_Tiling_790 d=new Domino_and_Tromino_Tiling_790(); System.out.println(d.numTilings(30)); } }
相关文章推荐
- [Leetcode] 790. Domino and Tromino Tiling 解题报告
- leetcode 790. Domino and Tromino Tiling
- LWC 73: 790. Domino and Tromino Tiling
- 73th LeetCode Weekly Contest Domino and Tromino Tiling
- 790. Domino and Tromino Tiling
- [LeetCode] Domino and Tromino Tiling 多米诺和三格骨牌
- 790. Domino and Tromino Tiling
- Weekly Contest 73 leetcode 791. Custom Sort String
- Leetcode73: Best Time to Buy and Sell Stock II
- Weekly Contest 73 leetcode 788. Rotated Digits
- Weekly Contest 73 leetcode 789. Escape The Ghosts
- 73_leetcode_Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 刷题之路 73 Best Time to Buy and Sell Stock III
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- 《leetCode》:Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode : Best Time to Buy and Sell Stock II
- Add and Search Word - Data structure design - LeetCode
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树 - Medium
- Leetcode: Remove Duplicates from Sorted Array I and II