-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay43.cpp
More file actions
50 lines (38 loc) · 973 Bytes
/
Day43.cpp
File metadata and controls
50 lines (38 loc) · 973 Bytes
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
/*
This problem was asked by Wayfair.
You are given a 2 x N board, and instructed to completely cover the board with the following shapes:
Dominoes, or 2 x 1 rectangles.
Trominoes, or L-shapes.
For example, if N = 4, here is one possible configuration, where A is a domino, and B and C are trominoes.
A B B C
A B C C
Given an integer N, determine in how many ways this task is possible.
*/
#include <iostream>
#include <vector>
using namespace std;
int numTilings(int n)
{
int MOD = 1e9 + 7;
if (n <= 2)
return n;
// Start from 3rd
long current = 5;
long prev = 2;
long prevPrev = 1;
// Formula : 2 * f(n - 1) + f(n - 3)
for (int i = 4; i <= n; i++)
{
long t = prev;
prev = current;
current = (2 * prev % MOD + prevPrev % MOD) % MOD;
prevPrev = t;
}
return (int)current;
}
int main()
{
int N = 10;
cout << "Number of tilings: " << numTilings(N) << endl;
return 0;
}