-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path526.py
41 lines (41 loc) · 1.49 KB
/
526.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
__________________________________________________________________________________________________
sample 44 ms submission
cache = {}
class Solution(object):
def countArrangement(self, N):
def helper(i, X):
if i == 1:
return 1
key = i, X
if key in cache:
return cache[key]
total = sum(helper(i - 1, X[:j] + X[j + 1:])
for j, x in enumerate(X)
if x % i == 0 or i % x == 0)
cache[key] = total
return total
return helper(N, tuple(range(1, N + 1)))
__________________________________________________________________________________________________
sample 13000 kb submission
class Solution:
def countArrangement(self, N: int) -> int:
if N < 2:
return 1
# i % nums[i] == 0 or
# num[i] % i == 0
result = [0]
visited = [False] * N
self.countHelper(N, visited, result)
return result[0]
def countHelper(self, N, visited, result):
# while N > 0:
# if N > 0:
if N == 0:
result[0] = result[0] + 1
return
for i in range(len(visited)):
if (N % (i+1) == 0 or (i+1) % N == 0) and (not visited[i]):
visited[i] = True
self.countHelper(N-1, visited, result)
visited[i] = False
__________________________________________________________________________________________________