-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0254-factor-combinations.py
More file actions
36 lines (31 loc) · 970 Bytes
/
0254-factor-combinations.py
File metadata and controls
36 lines (31 loc) · 970 Bytes
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
# time complexity: O(2 ^ (n ** 0.5))
# space complexity: O(2 ^ (n ** 0.5))
from typing import List
class Solution:
def getFactors(self, n: int) -> List[List[int]]:
factors = []
for i in range(2, n//2 + 1):
if n % i == 0:
factors.append(i)
result = []
def backtrack(start: int, balance: int, comb):
if start == len(factors):
return
if balance == 1:
if len(comb) > 1:
result.append(list(comb))
return
for i in range(start, len(factors)):
value = factors[i]
comb.append(value)
if balance % value == 0:
backtrack(i, balance//value, comb)
comb.pop()
backtrack(0, n, [])
return (result)
n = 1
print(Solution().getFactors(n))
n = 12
print(Solution().getFactors(n))
n = 37
print(Solution().getFactors(n))