File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ const input = parseInt (
2+ require ( 'fs' )
3+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
4+ . toString ( )
5+ . trim ( ) ,
6+ 10
7+ ) ;
8+
9+ function solution ( N ) {
10+ let answer = [ ] ;
11+ let arr = [ ] ;
12+ for ( let i = 1 ; i <= N ; i ++ ) {
13+ arr . push ( i ) ;
14+ }
15+
16+ function permute ( tempArr , remainArr ) {
17+ if ( remainArr . length === 0 ) {
18+ answer . push ( tempArr ) ;
19+ return ;
20+ }
21+
22+ for ( let i = 0 ; i < remainArr . length ; i ++ ) {
23+ const current = remainArr [ i ] ;
24+ const remaining = remainArr . slice ( 0 , i ) . concat ( remainArr . slice ( i + 1 ) ) ;
25+
26+ permute ( [ ...tempArr , current ] , remaining ) ;
27+ }
28+ }
29+
30+ permute ( [ ] , arr ) ;
31+
32+ const result = answer . map ( ( row ) => row . join ( ' ' ) ) ;
33+ result . forEach ( ( row ) => console . log ( row ) ) ;
34+ }
35+
36+ solution ( input ) ;
You can’t perform that action at this time.
0 commit comments