File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ ๋ฌธ์ : target์ด ๋ ๋๊น์ง candidates์ ์๋ฅผ ๋ํ๋ ๋ชจ๋ ์กฐํฉ์ ๊ตฌํ๋ผ.
3+ ํ์ด: ๋ฐฑํธ๋ํน์ ์ด์ฉํ์ฌ ์กฐํฉ์ ๊ตฌํ๋ค. ๋ง์ฝ ํ์ฌ ํฉ์ด target๊ณผ ๊ฐ๋ค๋ฉด ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ถ๊ฐํ๊ณ , ์๋ค๋ฉด ํ๋ณด๊ตฐ์ ์ํํ๋ฉฐ ์ฌ๊ท์ ์ผ๋ก ํ์ํ๋ค.
4+ ์๊ฐ๋ณต์ก๋: O(N^T), N์ ํ๋ณด๊ตฐ์ ์, T๋ target์ ๊ฐ
5+ ์ต์
์ ๊ฒฝ์ฐ, ํ๋ณด๊ตฐ์ ์ N๊ณผ target์ ๊ฐ T์ ๋ฐ๋ผ ๋ชจ๋ ์กฐํฉ์ ํ์ํด์ผ ํ๋ฏ๋ก ์๊ฐ๋ณต์ก๋๋ O(N^T)์ด๋ค.
6+ ๊ณต๊ฐ๋ณต์ก๋: O(T)
7+ ์ฌ๊ท ํธ์ถ ์คํ๊ณผ ํ์ฌ ์กฐํฉ์ ์ ์ฅํ๋ ๋ฐ ์ต๋ T๊ฐ์ ๊ณต๊ฐ์ด ํ์ํ๋ฏ๋ก ๊ณต๊ฐ๋ณต์ก๋๋ O(T)์ด๋ค.
8+ ์ฌ์ฉํ ์๋ฃ๊ตฌ์กฐ: ๋ฆฌ์คํธ
9+ ์ฐธ๊ณ ๋ก ํจ์์ ํธ์ถ ๊น์ด๋ target/min(candidates)๋ก ์ ํ๋๋ค.
10+ '''
11+
12+
13+ class Solution :
14+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
15+ candidates .sort ()
16+ s , now = [], []
17+
18+ def find (start , score ):
19+ if score == target :
20+ s .append (now [:])
21+ elif score < target :
22+ for i in range (start , len (candidates )):
23+ n = candidates [i ]
24+ now .append (n )
25+ find (i , score + n )
26+ now .pop ()
27+ find (0 , 0 )
28+ return s
29+
30+
You canโt perform that action at this time.
0 commit comments