Skip to content

Commit a636a16

Browse files
committed
64차 2번 문제풀이
1 parent fe3e5bc commit a636a16

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

live6/test64/문제2/임동민.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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();

0 commit comments

Comments
 (0)