forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombinationSumIII.swift
41 lines (33 loc) · 1.08 KB
/
CombinationSumIII.swift
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
/**
* Question Link: https://leetcode.com/problems/combination-sum-iii/
* Primary idea: Classic Depth-first Search
*
* Time Complexity: O(n!), Space Complexity: O(nCk)
*
*/
class CombinationSumIII {
func combinationSum3(k: Int, _ n: Int) -> [[Int]] {
let candidates = [Int](1...9)
var res = [[Int]](), path = [Int]()
dfs(&res, &path, candidates, n, 0, k)
return res
}
fileprivate func dfs(_ res: inout [[Int]], _ path: inout [Int], _ candidates: [Int], _ target: Int, _ index: Int, _ size: Int) {
if target == 0 && path.count == size {
res.append(Array(path))
return
}
guard path.count < size else {
return
}
for i in index..<candidates.count {
let candidate = candidates[i]
guard candidate <= target else {
break
}
path.append(candidate)
_dfs(&res, &path, candidates, target - candidate, i + 1, size)
path.removeLast()
}
}
}