File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
exercises/practice/killer-sudoku-helper/.meta Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ export const combinations = ( cage ) => {
2+ const { sum, size, exclude } = cage ;
3+ const result = [ ] ;
4+ const digits = [ ...Array ( 10 ) . keys ( ) ]
5+ . slice ( 1 )
6+ . filter ( ( d ) => ! exclude . includes ( d ) ) ;
7+
8+ function findCombinations ( remainingSum , index , combination ) {
9+ if ( remainingSum === 0 && combination . length === size ) {
10+ result . push ( combination ) ;
11+ return ;
12+ }
13+
14+ for ( let i = index ; i < digits . length ; i ++ ) {
15+ const digit = digits [ i ] ;
16+ if ( digit > remainingSum ) {
17+ break ;
18+ }
19+ if ( combination . includes ( digit ) ) {
20+ continue ;
21+ }
22+ findCombinations ( remainingSum - digit , i + 1 , [ ...combination , digit ] ) ;
23+ }
24+ }
25+
26+ findCombinations ( sum , 0 , [ ] ) ;
27+ return result ;
28+ } ;
You can’t perform that action at this time.
0 commit comments