File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
3
+ * - ์ค๋ณต ํฌํจํ์ฌ ๋ชจ๋ ์กฐํฉ ๊ตฌํด์ผ ํ๋๊น ์ฌ๊ทํจ์๋ก ํ๊ธฐ
4
+ * - ์ฌ๊ท ํธ์ถ๋ก ํ์ํด์ผํ๋ ํ๊ฒ ์ค์ฌ๊ฐ๋ฉด์ ์กฐํฉ ๋ง๋ค๊ธฐ
5
+ * - ๋์ผ ์กฐํฉ์ถ๊ฐ๋์ง ์๋๋ก, startIndex ์ถ๊ฐํ์ฌ ๋ค์ ์ธ๋ฑ์ค๋ถํฐ ์ํํ๋๋ก ์ ํ
6
+ *
7
+ *
8
+ * ์๊ฐ๋ณต์ก๋ : O(n^target)
9
+ * - candidates ๋ฐฐ์ด ๊ธธ์ด n๋งํผ ์ฌ๊ท๊ฐ ํธ์ถ๋๊ณ , ๊ฐ ํธ์ถ์ target ๊ธธ์ด ๋งํผ ์ค์ฒฉ๋๋๊น O(n^target)
10
+ *
11
+ * ๊ณต๊ฐ๋ณต์ก๋ : O(target)
12
+ * - ์ต์
์ ๊ฒฝ์ฐ target๋งํผ ์ฌ๊ท ํธ์ถ๋๋๊น O(target)
13
+ *
14
+ */
15
+
16
+ function combinationSum ( candidates : number [ ] , target : number ) : number [ ] [ ] {
17
+ const result : number [ ] [ ] = [ ] ;
18
+
19
+ const dfs = ( target : number , combination : number [ ] , startIndex : number ) => {
20
+ if ( target === 0 ) {
21
+ result . push ( [ ...combination ] ) ;
22
+ return ;
23
+ }
24
+
25
+ if ( target < 0 ) return ;
26
+
27
+ for ( let i = startIndex ; i < candidates . length ; i ++ ) {
28
+ combination . push ( candidates [ i ] ) ;
29
+ dfs ( target - candidates [ i ] , combination , i ) ;
30
+ combination . pop ( ) ;
31
+ }
32
+ } ;
33
+
34
+ dfs ( target , [ ] , 0 ) ;
35
+
36
+ return result ;
37
+ }
You canโt perform that action at this time.
0 commit comments