File tree Expand file tree Collapse file tree 3 files changed +87
-0
lines changed
Expand file tree Collapse file tree 3 files changed +87
-0
lines changed 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 ( '' )
6+ . map ( Number ) ;
7+
8+ function solution ( ) {
9+ input . sort ( ( a , b ) => b - a ) ;
10+ const sum = input . reduce ( ( a , b ) => a + b , 0 ) ;
11+
12+ if ( sum % 3 !== 0 || input [ input . length - 1 ] !== 0 ) {
13+ console . log ( - 1 ) ;
14+ } else {
15+ console . log ( input . join ( '' ) ) ;
16+ }
17+ }
18+
19+ solution ( ) ;
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+ . map ( e => e . split ( ' ' ) . map ( Number ) ) ;
7+
8+ function solution ( ) {
9+ const [ N , K ] = input [ 0 ] ;
10+ const testCase = input . slice ( 1 ) ;
11+ let sum = 0 ;
12+ const window = 2 * K + 1 ;
13+
14+ let maxPosition = 0 ;
15+
16+ for ( let i = 0 ; i < N ; i ++ ) {
17+ const [ , position ] = testCase [ i ] ;
18+ if ( position > maxPosition ) maxPosition = position ;
19+ }
20+
21+ const arr = new Array ( maxPosition + 1 ) . fill ( 0 ) ;
22+
23+ testCase . forEach ( ( [ ices , position ] ) => {
24+ arr [ position ] += ices ;
25+ } )
26+
27+ for ( let i = 0 ; i <= Math . min ( maxPosition , window - 1 ) ; i ++ ) {
28+ sum += arr [ i ] ;
29+ }
30+
31+ let max = sum ;
32+
33+ for ( let i = window ; i <= maxPosition ; i ++ ) {
34+ sum += arr [ i ] - arr [ i - window ] ;
35+ max = Math . max ( max , sum ) ;
36+ }
37+
38+ console . log ( max ) ;
39+ }
40+
41+ solution ( ) ;
Original file line number Diff line number Diff line change 1+ function solution ( cacheSize , cities ) {
2+ const stack = [ ] ;
3+ let time = 0 ;
4+
5+ if ( cacheSize === 0 ) {
6+ return cities . length * 5 ;
7+ }
8+
9+ cities . forEach ( e => {
10+ const lower = e . toLowerCase ( ) ;
11+ if ( stack . includes ( lower ) ) {
12+ time += 1 ;
13+ stack . splice ( stack . indexOf ( lower ) , 1 ) ;
14+ stack . push ( lower ) ;
15+ } else {
16+ time += 5 ;
17+
18+ if ( stack . length >= cacheSize ) {
19+ stack . shift ( ) ;
20+ }
21+
22+ stack . push ( lower ) ;
23+ }
24+
25+ } )
26+ return time ;
27+ }
You can’t perform that action at this time.
0 commit comments