File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,3 +38,42 @@ def dfs(target_list, start_i):
3838 dfs ([], 0 )
3939
4040 return results
41+
42+
43+ # 7기 풀이
44+ # T: target 수, C: candidates의 최소값일 때,
45+ # 시간 복잡도: O(n ^ (T/C))
46+ # - 최악의 경우는 가장 작은 candidate로 target을 만드는 횟수만큼 candidates를 탐색할 때이다. (dominant)
47+ # - sorting은 O(n log n)이며, 무시 가능
48+ # 공간 복잡도: O(T/C)
49+ # - dfs의 call stack의 깊이는 T/C가 최대 깊이
50+ # - result의 조합 또한 T/C가 최대 길이
51+ class Solution :
52+ # DFS로 문제를 풀이하며 조건에 맞춰 가지치기가 필요한 문제
53+ # 문제 풀이 편의를 위해 candidates를 sorting한 후 풀이를 한다.
54+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
55+ candidates .sort ()
56+ results = []
57+
58+ def dfs (target , result ):
59+ if target == 0 :
60+ # 더이상 target을 만드는데 숫자가 필요하지 않으므로
61+ # 정답 array에 deepcopy하여 저장
62+ results .append (result [:])
63+ return
64+
65+ for candidate in candidates :
66+ if candidate > target :
67+ # 후보 수가 target보다 크면 그 이후의 수는 더이상 탐색하지 않아도 된다
68+ return
69+ if result and candidate < result [- 1 ]:
70+ # 후보 수가 result의 마지막 수보다 크면 skip한다(result 내 숫자들도 작은 수부터 저장되게 함)
71+ continue
72+ result .append (candidate )
73+ # target에서 candidate를 뺀 만큼 다음 stack 계산을 한다.
74+ dfs (target - candidate , result )
75+ result .pop ()
76+
77+ dfs (target , [])
78+
79+ return results
You can’t perform that action at this time.
0 commit comments