File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Solution:
3+ ์ต์ด ํ์ด ๋น์์ ๋จ์ํ dfs๋ก ํ์์ผ๋ ์๊ฐ ์ด๊ณผ๋ก ์คํจํ์ต๋๋ค.
4+ ์ดํ ํ์ด ์ค๋ช
์ ํตํด i ๋ฒ์งธ ์ซ์๋ฅผ ๋ฃ๊ฑฐ๋ / ์๋ฃ๊ฑฐ๋ ๋ผ๋ ์กฐ๊ฑด์ผ๋ก i๋ฅผ ๋๋ ค๊ฐ๋๋ก ์งํํ๋ ๋ฐฑํธ๋ํน์ ํ๋ฉด ๋๋ค๋์ ์ ๋ฐฐ์ ์ต๋๋ค.
5+ ์ด๋ฅผ ํตํด ๋ถํ์ํ ์ค๋ณต์ ์ค์ด๊ณ ํจ์จ์ ์ธ ๊ตฌํ์ด ๊ฐ๋ฅํด์ง๋๋ค.
6+
7+ C: len(candidates)
8+ T: target size
9+
10+ Time: O(C^T) = ๋ผ๊ณ ์ค๋ช
๋์ด ์๋๋ฐ ์์ฐํ ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
11+ Space: O(T) = ์ฌ๊ท๊ฐ ๊ฐ์ฅ ๊น์ ๋ [1,1,1,1...] T ๋งํผ์ด๊ธฐ ๋๋ฌธ์ O(T)
12+ """
13+
14+
15+ class Solution :
16+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
17+ result = []
18+ sol = []
19+ n = len (candidates )
20+
21+ def backtrack (i , cur_sum ):
22+ if cur_sum == target :
23+ result .append (sol .copy ())
24+ return
25+ if cur_sum > target or i == n :
26+ return
27+
28+ backtrack (i + 1 , cur_sum )
29+
30+ sol .append (candidates [i ])
31+ backtrack (i , cur_sum + candidates [i ])
32+ sol .pop ()
33+
34+ backtrack (0 , 0 )
35+ return result
You canโt perform that action at this time.
0 commit comments