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