0
0

More than 1 year has passed since last update.

paizaラーニング レベルアップ問題集 線形探索メニュー応用編 JavaScript 区間探索 2

Posted at

区間探索 2 (paizaランク B 相当)

解答例

前問の区間探索 1の以上のところを、未満にします。

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

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

let max = 1;//最大長
let [l, r] = [-1, -1];//lとrの初期値。r-l+1=0なるように。

for (let i = 0; i < n; i++) {
  if (a[i] < x) { //aの要素がx未満
    //lが初期値のままだったら更新
    if (l < 0) {
      l = i;
    }
    //rは伸ばす
    r = i;
    //最後だったら、長さ判定する
    if (i === n - 1) {
      max = Math.max(max, r - l + 1);
    }
  } else {
    max = Math.max(max, r - l + 1);
    [l, r] = [-1, -1];//l,rリセット
  }
}
console.log(max);
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