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+ * @param - candidates: μ μ λ°°μ΄, target: λμ μ μ
3+ * @returns - μ νν μ«μμ ν©μ΄ ν΄λΉνλ λͺ¨λ κ³ μ μ‘°ν© λ°ν
4+ * @description
5+ * 1. μμλ μ¬λ¬λ² μ¬μ© κ°λ₯
6+ */
7+
8+ function combinationSum ( candidates : number [ ] , target : number ) : number [ ] [ ] {
9+ const result : number [ ] [ ] = [ ] ;
10+
11+ function recursive ( remain : number , idx : number , path : number [ ] ) {
12+ if ( remain === 0 ) {
13+ result . push ( [ ...path ] ) ;
14+ return ;
15+ }
16+
17+ for ( let i = idx ; i < candidates . length ; i ++ ) {
18+ const currentNum = candidates [ i ] ;
19+ if ( currentNum > remain ) {
20+ break ;
21+ }
22+
23+ path . push ( currentNum ) ;
24+ recursive ( remain - currentNum , i , path ) ;
25+ path . pop ( ) ;
26+ }
27+ }
28+
29+ recursive ( target , 0 , [ ] ) ;
30+ return result ;
31+ }
32+
33+ const candidates = [ 2 , 3 , 5 ] ;
34+ const target = 8 ;
35+ combinationSum ( candidates , target ) ;
You canβt perform that action at this time.
0 commit comments