forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
69 lines (56 loc) · 1.56 KB
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// Source : https://leetcode.com/problems/domino-and-tromino-tiling/description/
/// Author : liuyubobobo
/// Time : 2018-03-07
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
///
/// dp[i][0]: how many ways to fill a 2*(i-1) tiles plus:
/// .....0
/// .....0
///
/// dp[i][1]: how many ways to fill a 2*(i-1) tiles plus:
/// .....1
/// .....0
///
/// dp[i][2]: how many ways to fill a 2*(i-1) tiles plus:
/// .....0
/// .....1
///
/// dp[i][3]: how many ways to fill a 2*(i-1) tiles plus:
/// .....1
/// .....1
///
/// The answer is dp[N][3]
///
/// Time Complexity: O(N)
/// Space Complexity: O(N)
class Solution {
public:
int numTilings(int N) {
vector<vector<int>> res(N+1, vector<int>(4, 0));
res[0][0] = 0;
res[0][1] = 0;
res[0][2] = 0;
res[0][3] = 1;
int mod = 1000000007;
for(int i = 1 ; i <= N ; i ++){
res[i][0] = res[i-1][3];
res[i][1] = (res[i-1][0] + res[i-1][2]) % mod;
res[i][2] = (res[i-1][0] + res[i-1][1]) % mod;
res[i][3] = 0;
for(int j = 0 ; j < 4 ; j ++)
res[i][3] = (res[i][3] + res[i-1][j]) % mod;
// cout << res[i][0] << " " << res[i][1] << " " << res[i][2] << " " << res[i][3] << endl;
}
return res[N][3];
}
};
int main() {
cout << Solution().numTilings(3) << endl; // 5
cout << Solution().numTilings(4) << endl; // 11
cout << Solution().numTilings(7) << endl; // 117
cout << Solution().numTilings(1000) << endl; // 979232805
return 0;
}