Skip to content

Commit

Permalink
Update 22. Factor Combinations.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb authored Dec 4, 2024
1 parent 149bb4d commit a641645
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions 12_Backtracking/22. Factor Combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@

class Solution:
def get_factors(self, n: int) -> List[List[int]]:
if n <= 1:
return []

self.n = n
res = []
i = 2
while i*i <= n:
if n%i == 0:
res.append([i, n//i])
subres = self.get_factors(n//i)
for arr in subres:
if arr[0] >= i:
res.append([i] + arr)
i += 1

return res
def solve(n, i, tmp):
if n == 1:
res.append(tmp)
return
while i*i <= n:
if n%i == 0:
solve(n//i, i, tmp + [i])
i += 1
if n < self.n:
solve(n//n, n, tmp + [n])
solve(self.n, 2, [])
return res


# Time: O(sqrt(n) * log(n)) # https://algo.monster/liteproblems/254
Expand Down

0 comments on commit a641645

Please sign in to comment.