0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paizaラーニング レベルアップ問題集 線形探索メニュー応用編 JavaScript 連続する k 要素 1

Posted at

連続する k 要素 1 (paizaランク C 相当)

解答例

k個の和なので、和の先頭は、aのn-kまで調べます。和については、iからi+k-1までを求めます。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");

const [n, k] = lines[0].split(" ").map(Number);
const a = lines[1].split(" ").map(Number);

let ans = 0;
let max = -Infinity;
for (let i = 0; i <= n - k; i++) { //n-kまで
  let sum = 0;
  for (let j = i; j < i + k; j++) { //和はiからi+k-1まで
    sum += a[j];
  }
  if (sum > max) {
    max = sum;
    ans = i + 1;
  }
}
console.log(ans);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?