Skip to content

Commit

Permalink
[DFS] Refactor code style of Factor Combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Feb 28, 2020
1 parent dc2cae9 commit 0d7b4c4
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions DFS/FactorCombinations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,27 @@

class FactorCombinations {
func getFactors(_ n: Int) -> [[Int]] {
var res = [[Int]]()
var path = [Int]()
var paths = [[Int]](), path = [Int]()

dfs(&res, &path, n, 2)
dfs(&paths, path, 2, n)

return res
return paths
}

private func dfs(_ res: inout [[Int]], _ path: inout [Int], _ n: Int, _ start: Int) {
if n == 1 {
private func dfs(_ paths: inout [[Int]], _ path: [Int], _ start: Int, _ target: Int) {
if target == 1 {
if path.count > 1 {
res.append(Array(path))
paths.append(path)
}
return
}

if start > n {
guard start <= target else {
return
}

for i in start...n where n % i == 0 {
path.append(i)
dfs(&res, &path, n / i, i)
path.removeLast()
for factor in start...target where target % factor == 0 {
dfs(&paths, path + [factor], factor, target / factor)
}
}
}

0 comments on commit 0d7b4c4

Please sign in to comment.