Skip to content

Commit 5281984

Browse files
authored
Create m351.py - Weekly Premium BF
1 parent ab7231f commit 5281984

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

my-submissions/m351.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def numberOfPatterns(self, m: int, n: int) -> int:
3+
self.cases = [(1, 9), (3, 7),
4+
(1, 3), (4, 6), (7, 9),
5+
(1, 7), (2, 8), (9, 3),]
6+
7+
def dfs(prev: List[int], nxt: Set[int], minn: int, maxx: int) -> int :
8+
output = 0
9+
if minn <= len(prev) <= maxx :
10+
output += 1
11+
if not nxt or len(prev) >= maxx :
12+
return output
13+
14+
15+
temp = nxt.copy()
16+
for i in temp :
17+
if prev and ((prev[-1], i) in self.cases or (i, prev[-1]) in self.cases):
18+
if (prev[-1] + i) // 2 not in prev :
19+
continue
20+
21+
nxt.remove(i)
22+
prev.append(i)
23+
output += dfs(prev, nxt, m, n)
24+
nxt.add(prev.pop())
25+
26+
return output
27+
28+
return dfs([], set([1,2,3,4,5,6,7,8,9]), m, n)

0 commit comments

Comments
 (0)