-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
35 lines (32 loc) · 950 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const filePath = require('path').join(__dirname, 'input');
const [num, arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
const [n, k] = num.split(' ');
function solution (n, k, arr) {
// return Math.max(...arr.reduce((acc, cur, i) => {
// if (i < k) acc[0] += cur;
// else acc.push(acc[acc.length - 1] + cur - arr[i - k]);
// return acc;
// }, [0]));
// let sum = arr.slice(0, k).reduce((acc, cur) => acc + cur, 0);
// let max = sum;
// for (let i = k; i < n; i ++) {
// sum += arr[i] - arr[i - k];
// max = Math.max(max, sum);
// }
// return max;
let sum = 0;
for (let i = 0; i < k; i++) {
sum += arr[i];
}
let max = sum;
for (let i = k; i < n; i ++) {
sum += arr[i] - arr[i - k];
max = Math.max(max, sum);
}
return max;
}
console.log(solution(+n, +k, arr.split(' ').map(Number)));