[Leetcode] 790. Domino and Tromino Tiling 解题报告
2018-03-30 09:24
1036 查看
题目:
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.
(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.)
Note:
N will be in range
思路:
我们定义fe
表示2*n的格子有多少种铺法,定义fo
表示2*n + 1的格子有多少种铺法(也就是在2*n的格子上面含有一个格子),然后推导fe和fo的递推关系。对于fe的最顶层,我们分别有如下图所示的几种铺法,因此,可以得出递推关系为:
fe
= fe[n - 1] + fe[n - 2] + 2fe[n - 3] + 2fo[n - 3];
fo
= fo[n - 1] + fe[n - 1].
特别地,我们容易得知:fe[1] = 1, fe[2] = 2, fe[3] = 5, fo
4000
[1] = 2, fo[2] = 2, fo[3] = 4。因此就可以根据初始条件和递推关系写出基于动态规划的源代码了。
我们在下面写出的源代码的空间复杂度为O(N),时间复杂度为O(N)。但是注意到fe
和fo
也仅仅只和fe[n-1], fe[n-2], fe[n-3]以及fo[n-1], fo[n-2], fo[n-3]有关,所以还可以进一步将空间复杂度从O(n)优化到O(1)。读者可以自行实现了^_^。
更新:后来发现上面的递推公式还是推导复杂了,其实把fo(n) = fo(n - 1) + fe(n - 1)代入fe(n),可以得到更简单的递推公式:fe
= fe[n - 1] + fe[n - 2] + 2fo[n-2],甚至还可以进一步优化为fe
= fe[n - 1] + fo[n - 1] + fo[n-2],还可以进一步优化为fe
= fo
+ fo[n - 2]。这样写出来的代码应该就更简洁优雅了。
代码:
原有代码:
更新后的代码:
class Solution {
public:
int numTilings(int N) {
vector<long long> fe(max(3, N + 1), 0), fo(max(3, N + 1), 0);
long long mod = 1000000007;
fe[1] = 1, fe[2] = 2;
fo[1] = 1, fo[2] = 2;
for (int i = 3; i <= N; ++i) {
fo[i] = fo[i - 1] + fe[i - 1];
fe[i] = fo[i] + fo[i - 2];
fe[i] %= mod;
fo[i] %= mod;
}
return fe
;
}
};
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].
思路:
我们定义fe
表示2*n的格子有多少种铺法,定义fo
表示2*n + 1的格子有多少种铺法(也就是在2*n的格子上面含有一个格子),然后推导fe和fo的递推关系。对于fe的最顶层,我们分别有如下图所示的几种铺法,因此,可以得出递推关系为:
fe
= fe[n - 1] + fe[n - 2] + 2fe[n - 3] + 2fo[n - 3];
fo
= fo[n - 1] + fe[n - 1].
特别地,我们容易得知:fe[1] = 1, fe[2] = 2, fe[3] = 5, fo
4000
[1] = 2, fo[2] = 2, fo[3] = 4。因此就可以根据初始条件和递推关系写出基于动态规划的源代码了。
我们在下面写出的源代码的空间复杂度为O(N),时间复杂度为O(N)。但是注意到fe
和fo
也仅仅只和fe[n-1], fe[n-2], fe[n-3]以及fo[n-1], fo[n-2], fo[n-3]有关,所以还可以进一步将空间复杂度从O(n)优化到O(1)。读者可以自行实现了^_^。
更新:后来发现上面的递推公式还是推导复杂了,其实把fo(n) = fo(n - 1) + fe(n - 1)代入fe(n),可以得到更简单的递推公式:fe
= fe[n - 1] + fe[n - 2] + 2fo[n-2],甚至还可以进一步优化为fe
= fe[n - 1] + fo[n - 1] + fo[n-2],还可以进一步优化为fe
= fo
+ fo[n - 2]。这样写出来的代码应该就更简洁优雅了。
代码:
原有代码:
class Solution { public: int numTilings(int N) { vector<long long> fe(max(4, N + 1), 0), fo(max(4, N + 1), 0); long long mod = 1000000007; fe[1] = 1, fe[2] = 2, fe[3] = 5; fo[1] = 1, fo[2] = 2, fo[3] = 4; for (int i = 4; i <= N; ++i) { fe[i] = fe[i - 1] + fe[i - 2] + 2 * fe[i - 3] + 2 * fo[i - 3]; fo[i] = fo[i - 1] + fe[i - 1]; fe[i] %= mod; fo[i] %= mod; } return fe ; } };
更新后的代码:
class Solution {
public:
int numTilings(int N) {
vector<long long> fe(max(3, N + 1), 0), fo(max(3, N + 1), 0);
long long mod = 1000000007;
fe[1] = 1, fe[2] = 2;
fo[1] = 1, fo[2] = 2;
for (int i = 3; i <= N; ++i) {
fo[i] = fo[i - 1] + fe[i - 1];
fe[i] = fo[i] + fo[i - 2];
fe[i] %= mod;
fo[i] %= mod;
}
return fe
;
}
};
相关文章推荐
- Weekly Contest 73 leetcode 790. Domino and Tromino Tiling
- leetcode 790. Domino and Tromino Tiling
- 790. Domino and Tromino Tiling
- 73th LeetCode Weekly Contest Domino and Tromino Tiling
- LWC 73: 790. Domino and Tromino Tiling
- 790. Domino and Tromino Tiling
- [LeetCode] Domino and Tromino Tiling 多米诺和三格骨牌
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
- [Leetcode] 211. Add and Search Word - Data structure design 解题报告
- [Leetcode] 365. Water and Jug Problem 解题报告
- Leetcode 121. Best Time to Buy and Sell Stock 股票买卖 解题报告
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告
- [Leetcode] 188. Best Time to Buy and Sell Stock IV 解题报告
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- [Leetcode] 271. Encode and Decode Strings 解题报告
- LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range
- LeetCode-Best Time to Buy and Sell Stock IV -解题报告
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- 【LeetCode】Best Time to Buy and Sell Stock II 解题报告
- 【LeetCode】Best Time to Buy and Sell Stock 解题报告