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+ function solution ( sequence , k ) {
2+ let answer ;
3+ let l = 0 ;
4+ let sum = 0 ;
5+ let min = Number . MAX_SAFE_INTEGER ;
6+ for ( let r = 0 ; r < sequence . length ; r ++ ) {
7+ sum += sequence [ r ] ;
8+ while ( sum > k ) {
9+ sum -= sequence [ l ] ;
10+ l ++ ;
11+ }
12+ if ( sum === k ) {
13+ if ( r - l < min ) {
14+ min = r - l ;
15+ answer = [ l , r ] ;
16+ }
17+ }
18+ }
19+ return answer ;
20+ }
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+ . split ( "\n" ) ;
6+
7+ input . shift ( ) ;
8+
9+ const tmp = [ ] ;
10+
11+ for ( const i of input ) {
12+ const arr = i . split ( " " ) . map ( Number ) ;
13+ for ( const j of arr ) {
14+ tmp . push ( j ) ;
15+ }
16+ }
17+
18+ console . log ( tmp . sort ( ( a , b ) => a - b ) . join ( " " ) ) ;
Original file line number Diff line number Diff line change 1+ function solution ( n , lost , reserve ) {
2+ lost . sort ( ) ;
3+ reserve . sort ( ) ;
4+ let arr = [ ...lost ] ;
5+ lost . map ( ( item ) => {
6+ if ( reserve . includes ( item ) ) {
7+ arr . splice ( arr . indexOf ( item ) , 1 ) ;
8+ reserve . splice ( reserve . indexOf ( item ) , 1 ) ;
9+ }
10+ } ) ;
11+ let newArr = [ ...arr ] ;
12+
13+ for ( let i = 0 ; i < arr . length ; i ++ ) {
14+ if ( reserve . includes ( arr [ i ] - 1 ) || reserve . includes ( arr [ i ] + 1 ) ) {
15+ if ( reserve . includes ( arr [ i ] - 1 ) ) {
16+ reserve . splice ( reserve . indexOf ( arr [ i ] - 1 ) , 1 ) ;
17+ newArr . shift ( ) ;
18+ continue ;
19+ }
20+ reserve . splice ( reserve . indexOf ( arr [ i ] + 1 ) , 1 ) ;
21+ newArr . shift ( ) ;
22+ }
23+ }
24+ return n - newArr . length ;
25+ }
You can’t perform that action at this time.
0 commit comments