File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const input = require ( 'fs' )
2+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
3+ . toString ( )
4+ . trim ( ) ;
5+
6+ function solution ( input ) {
7+ const digits = input . split ( '' ) ;
8+ const visited = Array ( digits . length ) . fill ( false ) ;
9+ let found = Infinity ;
10+
11+ function dfs ( current ) {
12+ if ( current . length === digits . length ) {
13+ const num = parseInt ( current . join ( '' ) , 10 ) ;
14+ if ( num > parseInt ( input , 10 ) ) {
15+ found = Math . min ( found , num ) ;
16+ }
17+ return ;
18+ }
19+
20+ for ( let i = 0 ; i < digits . length ; i ++ ) {
21+ if ( ! visited [ i ] ) {
22+ visited [ i ] = true ;
23+ current . push ( digits [ i ] ) ;
24+ dfs ( current ) ;
25+ current . pop ( ) ;
26+ visited [ i ] = false ;
27+ }
28+ }
29+ }
30+
31+ dfs ( [ ] ) ;
32+
33+ return found === Infinity ? 0 : found ;
34+ }
35+
36+ console . log ( solution ( input ) ) ;
You can’t perform that action at this time.
0 commit comments