-
Notifications
You must be signed in to change notification settings - Fork 4
/
domino-and-tromino-tiling.py
113 lines (102 loc) · 3.14 KB
/
domino-and-tromino-tiling.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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 790. Domino and Tromino Tiling
# 🟠 Medium
#
# https://leetcode.com/problems/domino-and-tromino-tiling/
#
# Tags: Dynamic Programming
import timeit
from collections import deque
# We can see that the number of ways to place n tokens will be the
# same as the number of ways to place n-1 tokens with one more before
# and after it plus the ways to combine these two blocks, which is
# equivalent to the ways to place n-3 tokens. That means that, to
# compute the number of ways to place n tokens, we need to start with
# the known ways to place n=1, n=2, n=3 and keep computing higher values
# using these until we arrive at n. Since we only need the previous 3
# values at any point, we only need to store these, we can use a
# sliding window of values, implemented using a queue.
#
# Time complexity: O(n) - We only do one operation and push/pop into a
# queue at any given time.
# Space complexity: O(1) - We use constant space.
#
# Runtime 23 ms Beats 99.68%
# Memory 13.8 MB Beats 99.21%
class DP:
def numTilings(self, n: int) -> int:
q = deque([1, 2, 5])
if n < 4:
return q[n - 1]
for _ in range(3, n):
q.append(2 * q[-1] + q[0])
q.popleft()
return q[-1] % (10**9 + 7)
# Similar solution but avoiding the use of a queue.
#
# Time complexity: O(n) - We only do one operation and push/pop into a
# queue at any given time.
# Space complexity: O(1) - We use constant space.
#
# Runtime 34 ms Beats 94.76%
# Memory 13.8 MB Beats 99.21%
class DP2:
def numTilings(self, n: int) -> int:
dp = (1, 2, 5)
if n < 4:
return dp[n - 1]
for _ in range(3, n):
dp = (dp[1], dp[2], 2 * dp[-1] + dp[0])
return dp[-1] % (10**9 + 7)
# Similar solution using 3 variables.
#
# Time complexity: O(n) - We only do one operation and push/pop into a
# queue at any given time.
# Space complexity: O(1) - We use constant space.
#
# Runtime 34 ms Beats 94.76%
# Memory 13.9 MB Beats 85.40%
class DP3:
def numTilings(self, n: int) -> int:
if n < 3:
return n
if n == 3:
return 5
a, b, c = 1, 2, 5
for _ in range(3, n):
a, b, c = b, c, 2 * c + a
return c % (10**9 + 7)
def test():
executors = [
DP,
DP2,
DP3,
]
tests = [
[1, 1],
[2, 2],
[3, 5],
[4, 11],
[5, 24],
[6, 53],
[7, 117],
[8, 258],
[500, 603582422],
[1000, 979232805],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.numTilings(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()