Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit b8db789

Browse files
committed
created solution for problem62'
1 parent 664f25a commit b8db789

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

problem62/Solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def numTilings(self, n: int) -> int:
3+
MOD = 10**9 + 7
4+
# Memoization
5+
evenCache = [0] * (n+1)
6+
oddCache = [0] * (n+1)
7+
8+
# Place a domino
9+
def domino(N):
10+
if N == 0: return 1
11+
if evenCache[N]: return evenCache[N]
12+
13+
ways = domino(N-1) % MOD
14+
if N-1 > 0:
15+
ways += (domino(N-2) % MOD) + ((2 * tromino(N-2)) % MOD)
16+
17+
evenCache[N] = ways % MOD
18+
return evenCache[N]
19+
20+
# Place a tromino (previously a tromino was placed)
21+
def tromino(N):
22+
if N == 0: return 0
23+
if oddCache[N]: return oddCache[N]
24+
25+
ways = (tromino(N-1) % MOD) + (domino(N-1) % MOD)
26+
27+
oddCache[N] = ways % MOD
28+
return oddCache[N]
29+
30+
return domino(n) % MOD

0 commit comments

Comments
 (0)