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 要素 2

Posted at

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

# 解答例
最も大きいiを答えるので、最大値が等しくても、ansを更新する。

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++) {
  let sum = 0;
  for (let j = i; j < i + k; j++) {
    sum += a[j];
  }
  if (sum >= max) { //最も大きいiなので >=
    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?