0
0

More than 1 year has passed since last update.

paizaラーニング レベルアップ問題集 スタック・キューメニュー JavaScript 2 つのキュー

Last updated at Posted at 2023-01-16

2 つのキュー (paizaランク C 相当)

解答例

query,K,Xを文字列のまま処理していることに注意。

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

const [Q] = lines[0].split(" ").map(Number);

//2 つのキューを用意
let [queue1, queue2] = [[], []];

//クエリを順に処理
for (let i = 1; i <= Q; i++) {
  const [query, K, X] = lines[i].split(" ");//文字列のまま
  if (query === "1") {//文字列
    if (K === "1") {//文字列
      queue1.push(X);
    } else {
      queue2.push(X);
    }
  } else if (query === "2") {//文字列
    if (K === "1") {//文字列
      console.log(queue1.shift());//削除と返却を同時に
    } else {
      console.log(queue2.shift());
    }
  } else {
    console.log(queue1.length, queue2.length);
  }
 console.log(queue1,queue2);
}
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