-
Notifications
You must be signed in to change notification settings - Fork 57
/
Solution.py
38 lines (26 loc) · 1.01 KB
/
Solution.py
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
"""
You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return the number of ways to tile an 2 x n board. Since the answer may be very large, return it modulo 109 + 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 1:
Input: n = 3
Output: 5
Explanation: The five different ways are show above.
Example 2:
Input: n = 1
Output: 1
Constraints:
1 <= n <= 1000
"""
class Solution:
def numTilings(self, n: int) -> int:
@cache
def solve(i, previous_gap):
if i > n: return 0
if i == n: return not previous_gap
if previous_gap:
return solve(i+1, False) + solve(i+1, True)
return solve(i+1, False) + solve(i+2, False) + 2*solve(i+2, True)
return solve(0, False) % 1_000_000_007