# 69. 贪吃的猴子
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', function(line) {
let len = parseInt(line);
rl.on('line', function(line) {
let numbers = numbers.split(' ').map(Number);
rl.on('line', function(line) {
let N = parseInt(N);
console.log(maxBananas(numbers, N));
});
});
});
function maxBananas(numbers, N) {
let total = numbers.reduce((acc, val) => acc + val, 0);
if (N === numbers.length) {
return total;
}
let minSum = Infinity;
let currentSum = 0;
let windowSize = numbers.length - N;
for(let i=0; i<windowSize; i++) {
currentSum += numbers[i];
}
minSum = currentSum;
for(let i=windowSize; i<numbers.length; i++) {
currentSum += numbers[i] - numbers[i-windowSize];
minSum = Math.min(minSum, currentSum);
}
return total - minSum;
}
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
36
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
36