Skip to content

Commit

Permalink
Create 22. Factor Combinations.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Mar 20, 2024
1 parent 8cba6a3 commit bd4ce21
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 12_Backtracking/22. Factor Combinations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# https://www.lintcode.com/problem/1308


from typing import List

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

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


# Time: O(sqrt(n) * log(n)) # https://algo.monster/liteproblems/254
# Space: O(m + log(n)) where m is the number of combinations of factors.

0 comments on commit bd4ce21

Please sign in to comment.