File tree Expand file tree Collapse file tree 3 files changed +95
-0
lines changed
Expand file tree Collapse file tree 3 files changed +95
-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+
6+ function solution ( input ) {
7+ const numberStruct = input . split ( '' ) . map ( Number ) ;
8+
9+ if ( ! numberStruct . includes ( 0 ) ) return - 1 ;
10+
11+ const sum = numberStruct . reduce (
12+ ( totalSum , currentNumber ) => totalSum + currentNumber ,
13+ 0
14+ ) ;
15+ if ( sum % 3 !== 0 ) return - 1 ;
16+
17+ numberStruct . sort ( ( a , b ) => b - a ) ;
18+
19+ return numberStruct . join ( '' ) ;
20+ }
21+
22+ console . log ( solution ( input ) ) ;
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 ( ( el ) => el . split ( ' ' ) . map ( Number ) ) ;
7+
8+ function solution ( input ) {
9+ const [ N , K ] = input [ 0 ] ;
10+ const field = input . slice ( 1 ) ;
11+
12+ const maxPosition = 1000000 ;
13+ const ice = new Array ( maxPosition + 1 ) . fill ( 0 ) ;
14+
15+ for ( const [ gi , xi ] of field ) {
16+ ice [ xi ] += gi ;
17+ }
18+
19+ let maxSum = 0 ;
20+ let currentSum = 0 ;
21+
22+ for ( let i = 0 ; i <= Math . min ( 2 * K , maxPosition ) ; i ++ ) {
23+ currentSum += ice [ i ] ;
24+ }
25+ maxSum = currentSum ;
26+
27+ for ( let i = 1 ; i + 2 * K <= maxPosition ; i ++ ) {
28+ currentSum -= ice [ i - 1 ] ;
29+ currentSum += ice [ i + 2 * K ] ;
30+ maxSum = Math . max ( maxSum , currentSum ) ;
31+ }
32+
33+ return maxSum ;
34+ }
35+
36+ console . log ( solution ( input ) ) ;
Original file line number Diff line number Diff line change 1+ function solution ( cacheSize , cities ) {
2+ let answer = 0 ;
3+ let cityArr = [ ] ;
4+
5+ for ( let i = 0 ; i < cities . length ; i ++ ) {
6+ let current = cities [ i ] . toLowerCase ( ) ;
7+ let findCity = cityArr . find ( ( city ) => city === current ) ;
8+ if ( ! findCity ) {
9+ cityArr . push ( current ) ;
10+ if ( cityArr . length > cacheSize ) {
11+ cityArr . shift ( ) ;
12+ }
13+ answer += 5 ;
14+ } else {
15+ cityArr = cityArr . filter ( ( city ) => city !== current ) ;
16+ cityArr . push ( current ) ;
17+ answer ++ ;
18+ }
19+ }
20+
21+ return answer ;
22+ }
23+
24+ console . log (
25+ solution ( 3 , [
26+ 'Jeju' ,
27+ 'Pangyo' ,
28+ 'Seoul' ,
29+ 'NewYork' ,
30+ 'LA' ,
31+ 'Jeju' ,
32+ 'Pangyo' ,
33+ 'Seoul' ,
34+ 'NewYork' ,
35+ 'LA' ,
36+ ] )
37+ ) ;
You can’t perform that action at this time.
0 commit comments