-
Time complexity:
$O(N^t)$ -
target
์ ํฌ๊ธฐ t์candiates
์ ํฌ๊ธฐ N์ ๋ํ์ฌ, t๋งํผ์ ์ฌ๊ทํธ์ถ์ด ์ฐ์์ ์ผ๋ก ์ผ์ด๋ ์ ์๊ณ ๊ฐ ํจ์์์ ๋ฐฐ์ด ์ํ ๋น์ฉ N์ด ๋ฐ์ํ ์ ์๋ค.
-
-
Space complexity:
$O(N^t)$ -
target
์ ํฌ๊ธฐ t์candiates
์ ํฌ๊ธฐ N์ ๋ํ์ฌ, ๋ฐฑํธ๋ํน์์ ๋ง๋ค ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์ ๋งํผret
์ด ์ ์ฌ๋ ์ ์๋ค.
-
func combinationSum(candidates []int, target int) [][]int {
sort.Ints(candidates)
var combination func(candidates []int, target int) [][]int
combination = func(candidates []int, target int) [][]int {
if target < 0 {
return nil
}
if target == 0 {
return [][]int{{}}
}
ret := make([][]int, 0)
for i, c := range candidates {
items := combination(candidates[i:], target-c)
for _, item := range items {
ret = append(ret, append(item, c))
}
}
return ret
}
return combination(candidates, target)
}