We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0439ac7 commit af27ccbCopy full SHA for af27ccb
1 file changed
combination-sum/sadie100.ts
@@ -0,0 +1,28 @@
1
+/*
2
+dfs로 각 candidates를 돌면서 target과 같아지는 순간에 result를 반환한다.
3
+
4
+같은 조합이 중복됨을 피하기 위해 candidates를 정렬하고, 각 숫자보다 크거나 같은 candidate만 탐색한다.
5
6
+시간복잡도 : O(N^(T/min(c))) - N은 candidates의 수, T는 타겟, c는 candidates.
7
+타겟을 candidates의 최소값으로 나눈 값이 최대 깊이이므로 최악의 경우 해당 횟수만큼 반복해서 배열을 탐색함
8
+*/
9
10
+function combinationSum(candidates: number[], target: number): number[][] {
11
+ candidates.sort((a, b) => a - b)
12
+ const result = []
13
+ const search = (idx, nums, sum) => {
14
+ if (sum === target) {
15
+ result.push(nums)
16
+ return
17
+ }
18
+ for (let i = idx; i < candidates.length; i++) {
19
+ const num = candidates[i]
20
+ if (sum + num > target) return
21
+ search(i, [...nums, num], sum + num)
22
23
24
25
+ search(0, [], 0)
26
27
+ return result
28
+}
0 commit comments