File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-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 ( '\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 ( ) ;
You can’t perform that action at this time.
0 commit comments